in reply to Processing Multiple Array Elements

Like I said, I don't know how useful this is but I found the elements operator cool. Plus it was my first little hack at using prototypes and making something that behaves like a built in.

Below is an updated version that doesn't not copy the original array to memory, but rather inserts elements as they are removed.

Should be handy if you want to use this with very large arrays. One problem I see with this is that if you delete elements form the array from an enclosing statement, the original array will be changed as they won't be there to push into temporary storage.

use strict;
BEGIN{ my %array_copy = (); sub elements(\@;$){ my $no_gc = $_[0]; # Hold onto $_[0] so it doesn't get garbage + collected . $array_copy {$_[0]} = () unless defined $array_copy{$_[0]}; # +Assign empty list if new. $_[1] = 1 unless $_[1] ; # Get one element +if none specified. my @vals = splice(@{$_[0]}, 0, $_[1] ); # Pop elements. if (@vals){ # If elements to r +eturn push @{$array_copy{$_[0]}},@vals; # Push elements into temp + copy. return @vals; # then return them +. }else{ @$no_gc = @{$array_copy {$_[0]}}; # Array now restored. delete $array_copy{$_[0]}; # Free array copy return ; } } }


-Lee

"To be civilized is to deny one's nature."