in reply to sorting multidimensional arrays

It looks like you want to reverse each second-level array while keeping them in place in the first-level array. In that case, you can do something like this:

my @unsorted = ( [8,9,10], [6,5,4], [7,8,9] ); foreach my $inner(@unsorted) { $inner = [ reverse @{ $inner } ]; }

This works because of the special aliasing nature of a for-loop. The loop variable $inner is an alias for each array reference, so changing it results in changing the array reference inside @unsorted.

Replies are listed 'Best First'.
Re^2: sorting multidimensional arrays
by Roy Johnson (Monsignor) on Mar 15, 2005 at 22:59 UTC
    I think he wanted to sort them in descending order, not reverse them. It just happens that with the example data he gave, the two are the same.

    So I'll just note that substituting sort { $b <=> $a} for the reverse in your example works, too.

    Oh, and if he didn't want the original data affected:

    my @sorted = map {[sort { $b <=> $a } @$_]} @unsorted;

    Caution: Contents may have been coded under pressure.