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."

In reply to Re: Processing Multiple Array Elements by shotgunefx
in thread Processing Multiple Array Elements by shotgunefx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.