
Zhris
Enthusiast
Oct 10, 2013, 8:18 AM
Post #5 of 5
(20454 views)
|
Re: [az_perlberd] Numeric Sorting of tab delimited file
[In reply to]
|
Can't Post
|
|
Ah yes, Bill is right. I have assumed that the first columns values are unique. Heres another version, although could be designed more efficiently if thats an issue. - It has to split twice per sort. - It uses a counteractive map just to handle the last line with no newline char (you could manually append the newline and remove the map).
#!/usr/bin/perl use strict; use warnings FATAL => qw/all/; open my $ifh, '<', 'input.txt' or die "cannot open: $!"; open my $ofh, '>', 'output.txt' or die "cannot open: $!"; my $header = <$ifh>; print $ofh $header; print $ofh map { chomp $_; "$_\n" } sort customsort <$ifh>; close $ofh; close $ifh; sub customsort { my ($a_w) = split /\t/, $a, 2; my ($b_w) = split /\t/, $b, 2; $a_w <=> $b_w; } Chris
(This post was edited by Zhris on Oct 10, 2013, 8:18 AM)
|