CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sorting 3 columns

 



mschucht
New User

Jun 7, 2002, 4:33 AM

Post #1 of 2 (1352 views)
sorting 3 columns Can't Post

hi,
i have got the following problem:
there is a file with three columns relating to x,y and z coordinates, the following manner:
x y z
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
. . .
. . .
question:
how can i sort the columns in such a manner that
only the x and z column is left over in the following way
1 1 1
1 1 2
2 1 1
2 1 2
. . .
. . .

thanks very much


davorg
Thaumaturge / Moderator

Jun 9, 2002, 7:02 AM

Post #2 of 2 (1343 views)
Re: [mschucht] sorting 3 columns [In reply to] Can't Post

Something like this:

Code
#! /usr/local/bin/perl -w 

use strict;

my @data = map { [ split ] } <DATA>;

@data =
sort { $a->[0] <=> $b->[0] || $a->[2] <=> $b->[2] }
@data;

print "@$_\n" foreach @data;

__END__
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives