LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sort 2-d array in perl (https://www.linuxquestions.org/questions/programming-9/sort-2-d-array-in-perl-867054/)

monkeyorhunter 03-07-2011 03:48 PM

sort 2-d array in perl
 
hi

i have a 2-d array:


16 14 458
17 14 489
17 16 494
17 15 494
17 18 548
2 0 57
4 0 85
4 2 128
6 2 139
8 2 169
8 4 214
8 6 233
11 6 259
11 8 313
13 8 328

I want to sort it first by the third, and then the second column.

i have the following code but it doesn't seem to work.

any help would be much appreciated.

#!/usr/bin/perl
my $In = "trial.txt";
my $Out = "trialn.txt";
open (Data,"<$In");
open (Datan,">$Out");
my @Data = <Data>;
my @sorted = sort {$a->[2] <=> $b->[2] || $a->[0] <=> $b->[0]} @Data;
print Datan (@sorted);
close Data;
close Datan;

monkeyorhunter 03-07-2011 08:52 PM

never mind. Figured it out!

Thanks to anyone who read this though.


All times are GMT -5. The time now is 10:30 PM.