Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
use Data::Dumper; my @arr=(1,2,3,4,5,6,7,8,9,10); my $var; foreach(@arr){ print "foreach takes :"."$_\n"; $var=shift @arr; print "shift removes :"."$var\n"; } print Dumper \@arr;
In the above code I used foreach loop to remove the elements from the array "@arr" by using the shift operator, Initially the foreach loop will take list of values, but when the array gets modified the foreach also getting affected why? Is it wrong using shift with foreach ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: foreach problem with shift
by lakshmananindia (Chaplain) on Mar 04, 2009 at 10:39 UTC | |
|
Re: foreach problem with shift
by johngg (Canon) on Mar 04, 2009 at 12:10 UTC | |
|
Re: foreach problem with shift
by vinoth.ree (Monsignor) on Mar 04, 2009 at 10:35 UTC | |
by Corion (Patriarch) on Mar 04, 2009 at 10:40 UTC |