On Mon, May 06, 2002 at 03:17:39PM +0100, Richard Adams wrote:
> Hello,
>  I've got an array of arrays, and want to sort by the 3rd element of the
> subarray. I then want to print out the sorted array, showing the index and
> values. E.g.,

Hmm, works for me with the exception of...

> I've tried 
>     @sorted = {$a ->[2] <=> $b ->[2]} @AoA but this gives a "cannot

there's a 'sort' missing before the block:

    ---------- mostly your code ----------
    #!/usr/bin/perl

    use strict;
    use warnings;

    my @AoA = (
        [23.56, 65.2, 12.4],
        [13, 56, 87],
        [45,876, 23],
    );

    my @sorted = sort {$a->[2] <=> $b->[2]} @AoA;
    print "$_->[2]\n" foreach reverse @sorted;
    ---------- mostly your code ----------

gives me:

    ---------- snip ----------
    nijushiho:/tmp$ perl x.pl
    87
    23
    12.4
    nijushiho:/tmp$ 
    ---------- snip ----------

-- 
                       If we fail, we will lose the war.

Michael Lamertz                        |      +49 221 445420 / +49 171 6900 310
Nordstr. 49                            |                       [EMAIL PROTECTED]
50733 Cologne                          |                 http://www.lamertz.net
Germany                                |               http://www.perl-ronin.de 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to