Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

create 2-D array and then sort from max. to min.

by 3dog (Initiate)
on Jul 15, 2010 at 18:58 UTC ( [id://849831]=perlquestion: print w/replies, xml ) Need Help??

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 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 Corion (Patriarch) on Jul 15, 2010 at 18:59 UTC
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://849831]
Approved by ahmad
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found