I inserted a print statement into your code that may help you understand
what's happening:

my @ary = (0..1000);

for (@ary)
{
        if($_ <= 1000)
        {
                        print;
                shift @ary;
        }
}
print scalar @ary, "\n";


################## my comments:
the for loop looks at the first element (0), @ary gets shifted inside the
loop, then the for loop looks at the second element in @ary, which is now 2,
since the 0 was shifted off the front.  @ary is shifted again inside and
then the for loop looks at the third element remaining in @ary which is 4.
Eventually, the for loop looks at the 501st element remaining in @ary, 1000,
and shifts @ary one last time, leaving 500 elements in @ary (501..1000)

-----Original Message-----
From: Luke Bakken [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 21, 2002 2:55 PM
To: [EMAIL PROTECTED]
Subject: shift inside of for loop


Hi all,

I've noticed some interesting behavior when trying to shift an array that
you're iterating over. For example:

my @ary = (0..1000);

for (@ary)
{
        if($_ <= 1000)
        {
                shift @ary;
        }
}
print scalar @ary, "\n";

This prints 500 on my NT box with ActiveState perl.  I would naively
expect the shift to shift all elements of the array off, because they all
are less than or equal to 1000.

Am I expecting behavior to be defined that isn't?

Thanks,
Luke Bakken


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to