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

Hi, I have two questions:

1) how to create a two-dimensional array?

my data looks like this:

A BBL 0.2

B BBH 0.3

C BB 0.1

2) How to sort the 3 column from max to min

thanks!

-Steven
  • Comment on create 2-D array and then sort from max. to min.

Replies are listed 'Best First'.
Re: create 2-D array and then sort from max. to min.
by Corion (Patriarch) on Jul 15, 2010 at 18:59 UTC
Re: create 2-D array and then sort from max. to min.
by BrowserUk (Patriarch) on Jul 15, 2010 at 19:05 UTC
    #! perl; -slw use strict; use Data::Dump qw[ pp ]; my @a = ( [ qw[ A BBL 0.2 ] ], [ qw[ B BBH 0.3 ] ], [ qw[ C BB 0.1 ] ], ); my @ordered = sort{ $b->[ 2 ] <=> $a->[ 2 ] } @a; pp \@ordered; __END__ c:\test>junk.pl [["B", "BBH", "0.3"], ["A", "BBL", "0.2"], ["C", "BB", "0.1"]]
Re: create 2-D array and then sort from max. to min.
by TedPride (Priest) on Jul 15, 2010 at 19:07 UTC
    use Data::Dumper; use strict; my @arr; while (<DATA>) { s/\s+$//; push @arr, [split /\s+/, $_] if $_; } print Dumper(\@arr); @arr = sort { $b->[2] <=> $a->[2] } @arr; print Dumper(\@arr); __DATA__ A BBL 0.2 B BBH 0.3 C BB 0.1
      works great! thanks a lot!
Re: create 2-D array and then sort from max. to min.
by moritz (Cardinal) on Jul 15, 2010 at 20:18 UTC

    Perl 5 doesn't support true 2-dimensional array, only arrays holding array references. Those are documented in perllol.

    Perl 6 - links to (nearly) everything that is Perl 6.