With this sample data set, I have a different question. How can I rearrange columns such as this:
before: col1 col4 col5 col2 col6 col3 Abc 12.8 8 left 1 15.7 Def 13.8 9 top 0 19.7 gef 14.8 9 left 0 19.7 Dgf 12.3 9 right 4 99.6 cef 16.8 4 right 0 89.7 baf 32.8 7 bottom 5 79.8 efg 16.8 5 right 0 56.7 etg 12.8 2 left 7 34.7
after: col1 col2 col3 col4 col5 col6 Abc left 15.7 12.8 8 1 Def top 19.7 13.8 9 0 gef left 19.7 14.8 9 0 Dgf right 99.6 12.3 9 4 cef right 89.7 16.8 4 0 baf bottom 79.8 32.8 7 5 efg right 56.7 16.8 5 0 etg left 34.7 12.8 2 7
I thought one way to do it is to transpose the array, sort _ColumnName_, then transpose it back. Is this right way to do it?
Thanks,
Shiping
At 07:50 PM 1/29/2004 -0500, Hanson, Rob wrote:
I have to run, otherwise I would elaborate a bit.
The code is below. Check out the "perldoc perlreftut" for what the "[EMAIL PROTECTED]", "@{$row}", and "$a->[2]" means. Check out "perldoc -f sort" for what the "sort {...} @rows" means. And of course ask questions if you get stuck (but take a look at the docs first).
############ THE CODE ############
my @rows;
while (my $line = <DATA>) { chomp($line); my @cols = split(/\s+/, $line); push @rows, [EMAIL PROTECTED]; }
print "Sorted on 5th column:\n"; @rows = sort {$a->[4] <=> $b->[4]} @rows;
foreach my $row (@rows) { print "@{$row}\n"; }
print "Sorted on 3rd column:\n"; @rows = sort {$a->[2] <=> $b->[2]} @rows;
foreach my $row (@rows) { print "@{$row}\n"; }
__DATA__ Abc 12.8 8 "left" 1 15.7 Def 13.8 9 "top" 0 19.7 gef 14.8 9 "left" 0 19.7 Dgf 12.3 9 "right" 4 99.6 cef 16.8 4 "right" 0 89.7 baf 32.8 7 "bottom" 5 79.8 efg 16.8 5 "right" 0 56.7 etg 12.8 2 "left" 7 34.7
############ THE OUTPUT ############
$ perl sort.pl Sorted on 5th column: efg 16.8 5 "right" 0 56.7 Def 13.8 9 "top" 0 19.7 gef 14.8 9 "left" 0 19.7 cef 16.8 4 "right" 0 89.7 Abc 12.8 8 "left" 1 15.7 Dgf 12.3 9 "right" 4 99.6 baf 32.8 7 "bottom" 5 79.8 etg 12.8 2 "left" 7 34.7 Sorted on 3rd column: etg 12.8 2 "left" 7 34.7 cef 16.8 4 "right" 0 89.7 efg 16.8 5 "right" 0 56.7 baf 32.8 7 "bottom" 5 79.8 Abc 12.8 8 "left" 1 15.7 Dgf 12.3 9 "right" 4 99.6 Def 13.8 9 "top" 0 19.7 gef 14.8 9 "left" 0 19.7
-----Original Message----- From: Boon Chong Ang [mailto:[EMAIL PROTECTED] Sent: Thursday, January 29, 2004 7:38 PM To: [EMAIL PROTECTED] Subject: sort
Hi,
I want to write a perl script to do something like this
Abc 12.8 8 "left" 1 15.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
Dgf 12.3 9 "right" 4 99.6
cef 16.8 4 "right" 0 89.7
baf 32.8 7 "bottom" 5 79.8
efg 16.8 5 "right" 0 56.7
etg 12.8 2 "left" 7 34.7
Just say I want to sort the row based on the value on fifth or third column, any advice how to do so?
Thank you & best regards,
ABC
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>