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)


In reply to RE: Answer: How do I reverse this array? by extremely
in thread How do I reverse a nested array? by Anonymous Monk

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.