Re: CDBI::Iterator->first moves it's index pointer

[prev] [thread] [next] [Date index for 2004/11/10]

From: Rhesa Rozendaal
Subject: Re: CDBI::Iterator->first moves it's index pointer
Date: 21:31 on 10 Nov 2004
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

CDBI::Iterator->first moves it's index pointer
Rhesa Rozendaal 17:23 on 10 Nov 2004

Re: CDBI::Iterator->first moves it's index pointer
Rhesa Rozendaal 21:31 on 10 Nov 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52