LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2011, 03:48 PM   #1
monkeyorhunter
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
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;
 
Old 03-07-2011, 08:52 PM   #2
monkeyorhunter
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
never mind. Figured it out!

Thanks to anyone who read this though.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort an array rahulruns Programming 3 11-17-2009 05:50 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
Sort an array in python J_Szucs Programming 3 06-15-2008 04:33 PM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 07:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration