in reply to How do I reverse a nested array?

sub rev_arr { my $arr_ref = shift; @$arr_ref = map { ref eq 'ARRAY' ? rev_arr($_) : $_ } reverse @$ar +r_ref; return $arr_ref; }

Replies are listed 'Best First'.
RE: Answer: How do I reverse this array?
by Anonymous Monk on Sep 26, 2000 at 22:24 UTC
    Erm, sorry that post looks unreadable. Should have RTFM better... Is there a good way of editing it?

    -Ted
RE: Answer: How do I reverse this array?
by extremely (Priest) on Sep 27, 2000 at 08:35 UTC

    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)