in reply to Processing Multiple Array Elements
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.
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 ; } } }
|
|---|