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

I am having a mindshagg with this..
#!/usr/bin/perl -w use strict; my $anon_array = [qw(one two three)]; # pass it to mysub mysub({ list => $anon_array }); #now pass it to mysub again, but reversed... # i can't figure out how to take an anon array and # pass it reversed on the spot... mysub({ list => \(reverse @{$anon_array}) }); sub mysub { my $args = shift; for ( @{$args->{list}} ){ print $_."\n"; } return; }

Basically.. I have an annon array, i can use it - send it as argument around my code.. how would i reverse it as it is being sent as an argument???

  • Comment on how to reverse an array refference and return an array reference on the spot
  • Download Code

Replies are listed 'Best First'.
Re: how to reverse an array refference and return an array reference on the spot
by friedo (Prior) on Aug 23, 2006 at 14:27 UTC
    You already know how to make a new anonymous array, so just make one and reverse the original array inside it:

    mysub( { list => [ reverse @{ $anon_array } ] } );
      Or even:
      mysub( { list => [ reverse @$anon_array ] } );
Re: how to reverse an array reference and return an array reference on the spot
by davorg (Chancellor) on Aug 23, 2006 at 14:29 UTC
    mysub({ list => [reverse @{$anon_array}] });

    Derefence the array, reverse it, then copy it into a new anonymous array.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      For your information: In contrast to [@stuff], the construct \(@stuff) returns a list of references to the elements of @stuff.