wrote:
>
> []$ perl
> $a = [1,2,3,4,5,6];
> foreach(@{$a}){
> my $b = unshift @{$a};
> my $c = shift @{$a};
> print $b,$c;
> }
> 615243
>
> That is ok but:
>
> []$ perl
> $a = [1,2,3,4,5,6];
> foreach(@{$a}){
> my $b = shift @{$a};
> my $c = shift @{$a};
> print $b,$c;
> }
> 1234
>
>
> Why are 5 and 6 missing?
perldoc perlsyn
[snip]
Foreach Loops
The "foreach" loop iterates over a normal list value and sets the
variable VAR to be each element of the list in turn. If the variable
is preceded with the keyword "my", then it is lexically scoped, and is
therefore visible only within the loop. Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop. If the variable was previously declared with "my", it uses
that variable instead of the global one, but it's still localized to
the loop. This implicit localisation occurs only in a "foreach" loop.
The "foreach" keyword is actually a synonym for the "for" keyword, so
you can use "foreach" for readability or "for" for brevity. (Or
because the Bourne shell is more familiar to you than csh, so writing
"for" comes more naturally.) If VAR is omitted, $_ is set to each
value.
If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. Conversely, if any element of LIST is NOT an lvalue,
any attempt to modify that element will fail. In other words, the
"foreach" loop index variable is an implicit alias for each item in the
list that you're looping over.
If any part of LIST is an array, "foreach" will get very confused if
you add or remove elements within the loop body, for example with
"splice". So don't do that.
^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^
John
--
use Perl;
program
fulfillment