4.11. Processing Multiple Elements of an ArraySolution# remove $N elements from front of @ARRAY (shift $N) @FRONT = splice(@ARRAY, 0, $N); # remove $N elements from the end of the array (pop $N) @END = splice(@ARRAY, -$N); DiscussionIt's often convenient to wrap these as functions: sub shift2 (\@) { return splice(@{$_[0]}, 0, 2); } sub pop2 (\@) { return splice(@{$_[0]}, -2); } This makes their behavior more apparent when you use them: @friends = qw(Peter Paul Mary Jim Tim); ($this, $that) = shift2(@friends); # $this contains Peter, $that has Paul, and # @friends has Mary, Jim, and Tim @beverages = qw(Dew Jolt Cola Sprite Fresca); @pair = pop2(@beverages); # $pair[0] contains Sprite, $pair[1] has Fresca, # and @beverages has (Dew, Jolt, Cola)
These two functions are prototyped to take an array reference as their argument to better mimic the built-in $line[5] = \@list; @got = pop2( @{ $line[5] } );
This is another example of where a proper array and not a mere list is called for. The See Also
The |
|