Re: CDBI::Iterator->first moves it's index pointer
[prev]
[thread]
[next]
[Date index for 2004/11/10]
Tony Bowden wrote:
> On Wed, Nov 10, 2004 at 06:23:15PM +0100, Rhesa Rozendaal wrote:
>
>>I expected it to return the first element, and not move its index
>>pointer.
>
> That would be weird.
>
> Consider:
>
> my $it = run_some_search();
>
> print $it->first;
> print $it->next;
> print $it->first;
> print $it->next;
>
> Would you really expect this to print 1, 2, 1, 3? I'd say most people
> would expect 1, 2, 1, 2.
No, I expexted it wouldn't move the pointer at all. I would have
expected 1,1,1,2 in the situation I was in, which looked something like
this:
if(!$it or $it->count==0 or $it->first->date < '2004-01-01') {
return ;
}
while(my $el = $it->next) {
# loop over all elements
}
However, when you put it like that ("first, next") I tend to go for 1,2
as well. But what would you expect if you did "next, next, next, wait a
minute, what's the first again? ok, next, next"?
Something like this
while(my $el = $it->next) {
next if $el->field == $it->first->field;
# do something with different elements
}
would loop endlessly, which might be unexpected. As it stands out, this
is a bad shortcut, but I didn't think it would be at first.
> I'm open to opinions here.
I'm not asking for a change, I was just surprised about the "reset,
next" in first. All I needed was a $it->reset after the if().
Rhesa