Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

sort based on column values

by patric (Acolyte)
on Jun 22, 2009 at 04:21 UTC ( #773475=perlquestion: print w/replies, xml ) Need Help??

patric has asked for the wisdom of the Perl Monks concerning the following question:

dear monks, i have a file like this.
cng003 sfd009 61.7 654 231 4 654 1800 711811 876112 E-193 cng006 sfd009 87.5 556 341 0 165 491 6234 45112 E-145 cng009 sfd009 80.2 234 536 3 17 184 9812345 9923000 E-13 The desired result is: cng006 sfd009 87.5 556 341 0 165 491 6234 45112 E-145 cng003 sfd009 61.7 654 231 4 654 1800 711811 876112 E-193 cng009 sfd009 80.2 234 536 3 17 184 9812345 9923000 E-13
the columns are seperated by tab space.considering the columns starts from one, the sort should be based on the 9th column. i know only to sort based on array. i dont have any idea to split the array and sort by its column values. please guide. thank you !!!

Replies are listed 'Best First'.
Re: sort based on column values
by ikegami (Pope) on Jun 22, 2009 at 04:37 UTC

    Create an array where each element represents a row as an array of fields.

    my @lines; while (<>) { chomp; push @lines, [ $_, split /\t/ ]; } @lines = sort { $a->[9] <=> $b->[9] } @lines; for (@lines) { print "$_->[0]\n"; }

    It can be written more tersely as:

    print map "$_->[0]\n", sort { $a->[9] <=> $b->[9] } map [ $_, split /\t/ ], map { chomp( my $s = $_; ); $s } <>;
Re: sort based on column values
by perliff (Monk) on Jun 22, 2009 at 09:40 UTC
    The inbuilt sort function is quite powerful, but syntax can be sometimes too complicated for simple things like sorting a tab delimited file. Fortunately, there is a cpan module Sort::Fields, that has a method with an extremely straightforward syntax. The basic function in the module is fieldsort, that can take three arguments, first, the delimiter ( in your case tab ), then which columns you want to sort on ( it can take multiple columns, and you can specify reverse, numerical sorts ) and finally the array containing the data. There is no need to worry about anything else. I deal with tab-delimited data frequently, and this module saves my skin all the time. Finally after all the blabber... here's how your data can be sorted ...
    use strict; use Sort::Fields; use Data::Dumper; open (INP,"data.txt") || die "cant open data.txt !"; my @data = <INP>; chomp (@data); print "before sorting...\n"; print Dumper @data; my @sorted = fieldsort '\t', [10], @data; print "\nafter sorting\n"; print Dumper @sorted;
    Note that fieldsort counts columns starting from 1 and not from 0.

    hope this helps!

    perliff

    ----------------------

    -with perl on my side

    "If you look at the code too long, the code also looks back at you"

Re: sort based on column values
by irah (Pilgrim) on Jun 22, 2009 at 04:34 UTC
    #!/usr/bin/perl use strict; use warnings; sub sortby_multiplecolumn { my ($val1, $val2, $val3, $val4, $val5, $val6, $val7, $val8, $val9) + = split(" ",$a); my ($val11, $val12, $val13, $val14, $val15, $val16, $val17, $val18 +, $val19) = split(" ",$b); $val9 <=> $val19; } print $_ foreach (sort sortby_multiplecolumn <STDIN>);

    You will get what you want. Here I used space as a delimiter. You can use tab as delimiter or use '\s'.

Re: sort based on column values
by bichonfrise74 (Vicar) on Jun 22, 2009 at 21:33 UTC
    This is a good example to use the Schwartzian Transform.
    #!/usr/bin/perl use strict; my $string; while( <DATA> ) { $string = $string . join " ", $_; } my $test = join "\n", map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, (split)[9]] } split( /\n/, $string); print $test; __DATA__ cng003 sfd009 61.7 654 231 4 654 1800 711811 876112 E-193 cng006 sfd009 87.5 556 341 0 165 491 6234 45112 E-145 cng009 sfd009 80.2 234 536 3 17 184 9812345 9923000 E-13

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://773475]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2021-12-09 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?