I basically wanted to print out submitted form values in a table with two parameters per table row.I had an array of the params so I was wishing I could just say

while (my (@elements)= (some_num_elements of @array){
   print Tr( Td(@elements));
}

Well I was going to use the Perl Cookbook solution but I didn't want to destroy the array shifting multiple elements off. I know I could have extremely easily stored it in an copy and used that but it got me thinking there must be a way to do it. Next thing I know, I had spent a couple of hours on a stupid little detail that could have been coded long hand in about five minutes because the look of an unimportant page vexed me. Oh well.

Here's what I came up with.
#!/usr/bin/perl -w use strict; BEGIN{ my %array_copy = (); # Storage of pristine arrays sub elements(\@;$){ my $no_gc = $_[0]; # Hold onto $_[0] so it doesn't get garbage + collected . my @t = @{$_[0]}; # Temp array as I can't get it to assign to + $array_copy directly. # ie $array_copy {$_[0]} = [ @{$_[0]} ] # Am I missing something? $array_copy {$_[0]} = [@t] unless defined $array_copy{$_[0]}; +# Store if we haven't. my @temp = @{$array_copy {$_[0]}}; $_[1] = 1 unless $_[1] ; # Get one element +if non specified. my @vals = splice(@{$_[0]}, 0, $_[1] ); # Pop elements. if (@vals){ # If elements to r +eturn return @vals; # then return them +. }else{ # Time to restore array back to it's previous self. # Tried @$_[0] = @{$array_copy {$_[0]}} but wouldn't work. # Seemed to temporarily change the reference, only for thi +s statement. @$no_gc = @{$array_copy {$_[0]}}; # Array now restored. delete $array_copy{$_[0]}; # Free array copy return ; } } } # The following is a silly example but it shows that calls to elements + can be # nested. Also, passing an element count is optional, it will default +to 1 element my @array1 = (1..15); my @array2 = (1..5); print "\@array1",join(", ", @array1),"\n"; print "\@array2",join(", ", @array2),"\n\n"; while (my @v = elements(@array1,3)){ print join(", ", @v),"\n"; print "-" x 30,"\n"; # Removal of elements from @array are visible here. while (my @x = elements(@array2,@array1) ){ # As this demonstr +ates. print "\t",join(", ", @x),"\n"; print "\t","_" x 30,"\n"; } } print "\@array1",join(", ", @array1),"\n"; print "\@array2",join(", ", @array2),"\n";


I'd be interested if anyone sees any problems with this or ways to improve it, hell I'd be interested to know if anyone could think of any uses for it. For me, it will be handy for spewing tables.

-Lee

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

In reply to 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.