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: General Discussions: General Questions: Re: [az_perlberd] Numeric Sorting of tab delimited file: Edit Log



Zhris
Enthusiast

Oct 10, 2013, 8:18 AM


Views: 21747
Re: [az_perlberd] Numeric Sorting of tab delimited file

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).


Code
#!/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)


Edit Log:
Post edited by Zhris (Enthusiast) on Oct 10, 2013, 8:18 AM


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

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