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:
Numeric Sorting of tab delimited file

 



az_perlberd
New User

Oct 9, 2013, 7:40 PM

Post #1 of 5 (10228 views)
Numeric Sorting of tab delimited file Can't Post

Greetings, Need help, read input file:
w r t y
6 re 5 6
5 ee 4 2
2 54 3 2
3 ew 1 1

I need to sort numerically only column 0 starting at row 1(6). And output all the entire lines into another file.
So my $desfile shoud look like this:
w r t y
2 54 3 2
3 ew 1 1
5 ee 4 2
6 re 5 6

thanks a lot


Zhris
Enthusiast

Oct 10, 2013, 4:19 AM

Post #2 of 5 (10224 views)
Re: [az_perlberd] Numeric Sorting of tab delimited file [In reply to] Can't Post

Hi,

How about something like the following:


Code
#!/usr/bin/perl  
use strict;
use warnings FATAL => qw/all/;

my %hash;

open my $ifh, '<', 'input.txt' or die "cannot open: $!";
my $header = <$ifh>;
while ( my $line = <$ifh> )
{
chomp $line;
my ($w) = split /\t/, $line, 2;
$hash{$w} = $line;
}
close $ifh;

open my $ofh, '>', 'output.txt' or die "cannot open: $!";
print $ofh $header;
print $ofh "$hash{$_}\n" foreach (sort { $a <=> $b } keys %hash);
close $ofh;


Chris


az_perlberd
New User

Oct 10, 2013, 5:35 AM

Post #3 of 5 (10220 views)
Re: [Zhris] Numeric Sorting of tab delimited file [In reply to] Can't Post

thanks Chris,
I'll try it in a while.
Rodney


BillKSmith
Veteran

Oct 10, 2013, 7:11 AM

Post #4 of 5 (10217 views)
Re: [Zhris] Numeric Sorting of tab delimited file [In reply to] Can't Post

There is no single answer to sort questions. The best solution depends on your data. The data that you show will sort correctly with perl's default sort. This is probably not what you want because it is only sure of working correctly if the numbers in col 1 have only one digit.

Chris's solution is excellent. It will work for any number, but each number must be unique.

An even more general solution is the "Schwartzian Transform". Refer to the FAQ "How do I sort an array"
Good Luck,
Bill


Zhris
Enthusiast

Oct 10, 2013, 8:18 AM

Post #5 of 5 (10213 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).


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)

 
 


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

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