http://www.perlmonks.org?node_id=33872

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to reverse an array:
my @ary = (1,2,[a,b,[],[3,4,[6]],c],45); my @rev = reverse map [ reverse @$_ ], @ary;
But my output is: ([],[c,[[6],4,3],[],b,a,[],[]) How do I fix this?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I reverse this array?
by chromatic (Archbishop) on Sep 25, 2000 at 06:45 UTC
    You need a recursive solution to reverse internal array references. You'll have to check for them with the ref operator. The following (untested) code might come in handy.
    sub reverse_array { my $arr_ref = shift; my $rev_array = []; foreach my $item (@$arr_ref) { if (ref($item) eq 'ARRAY') { $item = reverse_array($item); } unshift @$rev_array, $item; } return $rev_array; }

    Originally posted as a Categorized Answer.

Re: How do I reverse a nested array?
by extremely (Priest) on Sep 26, 2000 at 03:56 UTC
    sub rev_arr { my $ar = shift; ref $ar eq 'ARRAY' ? [ map rev_arr($_), reverse @$ar ] : $_ }
Re: How do I reverse a nested array?
by Anonymous Monk on Sep 26, 2000 at 22:20 UTC
    sub rev_arr { my $arr_ref = shift; @$arr_ref = map { ref eq 'ARRAY' ? rev_arr($_) : $_ } reverse @$ar +r_ref; return $arr_ref; }
      Erm, sorry that post looks unreadable. Should have RTFM better... Is there a good way of editing it?

      -Ted

      Your first variation seem to destroy the array that is handed to it. Dump the original array before and after your reverse and see. Fast, but kinda rude. =)

      Your second variation seems to do it in line, reversing the original array! I hadn't thought about making one that does it all in place! =)

      Your third variant was the one that left the original undisturbed.

      I really like the map variation, and I like better never making a named variable:

      use Data::Dumper; my @names = (1, 2, 3, [4, 5, 6, [7, 8, 9], 10], 11, 12, [13, 14] ); print Dumper(@names); my $reved = rev_arr5(\@names); print Dumper(@names); print Dumper(@$reved); # less than 80 col! yay! sub rev_arr5 { return [map { ref eq 'ARRAY' ? rev_arr5($_) : $_ } reverse @{shi +ft()}]; } sub rev_arr2a { @{$_[0]} = map { ref eq 'ARRAY' ? rev_arr2a($_) : $_ } reverse @ +{$_[0]}; } sub rev_arr4 { my $arr = [reverse @{shift()}]; foreach (@$arr) { if ( ref $_ eq 'ARRAY' ) { $_ = rev_arr4 ($_); } } return $arr; }

      That version is safe from unintended effects (plays well with others) and is lickety-split. BTW I ran 1 million reps of eash thru Benchmark::cmpthese so really this is a waste to optimize anyway. =) I also tested a variant of Ted's number 2 that has no return at the end, since it modifies in place anyway.

                         Rate Extremely_orig Extremely_4 Ted_var_3 Extremely_5 Ted_var_2 Ted_var_2a
      Extremely_orig 106045/s             --         -3%      -28%        -46%      -47%       -51%
      Extremely_4    109170/s             3%          --      -26%        -45%      -45%       -50%
      Ted_var_3      147059/s            39%         35%        --        -25%      -26%       -32%
      Extremely_5    196850/s            86%         80%       34%          --       -1%        -9%
      Ted_var_2      199601/s            88%         83%       36%          1%        --        -8%
      Ted_var_2a     217391/s           105%         99%       48%         10%        9%         --
      

      My/Ted's one liner beats all the other variations that protect the original array quite handily. Ted's cool in place matches the one liner(ish) and removing the return nets you a small but not insignificant speedup. My variation 4 just shows the gain from one less assignment.

      Fun Stuff! Doh! last minute change, this variant:

      # Extremely_5a 209644/s sub rev_arr5a { return [map { ref eq 'ARRAY' ? rev_arr5a($_) : $_ } reverse @{$_ +[0]}]; }

      exploits the "no shift" trick to beat even the regular in place Ted_var_2 by 4%, Extremely_5 by 5%, and comes in about 6% under the in place Ted_var_2a.

      Benchmark is sucking my will to live. I'm gonna be here all night if I don't stop!

      --
      $you = new YOU;
      honk() if $you->love(perl)