in reply to pop an array
We read in the docs about a foreach loop:
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn.and :
In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over..So there is no need to stuff your $item variable with a pop-ed value from the @listoitems array. You will not only overwrite what was already in $item, but you will at the same time replace the array-element to which $item was aliased to with the value just pop-ed from the array and you will have shortened the array by one (which is an unescapable side-effect of pop.
The docs warn you about changing the length of the array you are working with:
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.
Try this little program and you will see how your script affects the array:
use Data::Dumper; @array = 0 ... 9; foreach $item (@array) { print ++$i . " through the loop\n"; $item = pop @array; print Dumper(\@array); }
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
|
|---|