Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Sort by specific column

by mraja_23 (Initiate)
on Mar 05, 2009 at 16:16 UTC ( [id://748573]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
I want to sort a file by a specific column. In the below case, could i know how would I sort by 2nd column using PERL?
02/28/2009|02062672169||4.99|9801|1.0|
02/28/2009|78513836100||4.96|9639|1.0|
02/28/2009|71171973992||19.99|9749|1.0|
02/28/2009|01463314931||19.99|9802|1.0|

Thanks for your help, in advance.
-Raja

Replies are listed 'Best First'.
Re: Sort by specific column
by moritz (Cardinal) on Mar 05, 2009 at 16:20 UTC
    Sure you could, for example by reading the documentation. There are also plenty of similar examples here at the monastery, try Super Search to find them.
Re: Sort by specific column
by zentara (Archbishop) on Mar 05, 2009 at 16:25 UTC
Re: Sort by specific column
by kyle (Abbot) on Mar 05, 2009 at 17:41 UTC
Re: Sort by specific column
by Anonymous Monk on Mar 05, 2009 at 16:54 UTC

    As you will see in your research, the built-in sort facility (as well as various CPAN modules) provide the means to supply your own “comparison function,” whose job is to determine the relative ordering of two records. This comparison function will of course be called thousands of times.

    Having said that, I'd also point out that the sort commands of many operating systems (including Linux/Unix) provide many options ... so that you may well find that you have no need for “your own comparison function.” If the standard Unix sort command is told that:

    1. I want to sort by the second field...
    2. I want to do a numeric comparison...
    3. The field-separator is “a vertical bar” ...
    ... then it can probably do exactly what you want, no programming required.

    Your research will quickly confirm that this is a very routine and straightforward requirement. It is well worth the time to acquaint yourself with the plethora of existing text-handling tools that are available.

Re: Sort by specific column
by GrandFather (Saint) on Mar 05, 2009 at 19:52 UTC

    sort can take a block which dictates the sort criteria. Consider:

    use strict; use warnings; use Text::xSV; my $data = <<'END_DATA'; 02/28/2009|02062672169||4.99|9801|1.0| 02/28/2009|78513836100||4.96|9639|1.0| 02/28/2009|71171973992||19.99|9749|1.0| 02/28/2009|01463314931||19.99|9802|1.0| END_DATA open my $inFile, '<', \$data or die "Failed to open data file: $!"; my $csv = Text::xSV->new (fh => $inFile, sep => '|'); my @rows; push @rows, $_ while $_ = $csv->get_row; @rows = sort {$a->[1] <=> $b->[1]} @rows; print join "\n", map {join '|', map {$_ || ''} @$_} @rows;

    Prints:

    02/28/2009|01463314931||19.99|9802|1.0| 02/28/2009|02062672169||4.99|9801|1.0| 02/28/2009|71171973992||19.99|9749|1.0| 02/28/2009|78513836100||4.96|9639|1.0|

    True laziness is hard work
Re: Sort by specific column
by Tanktalus (Canon) on Mar 05, 2009 at 22:01 UTC

    You have a table. Treat it as such.

    #! /usr/bin/perl -l use strict; use warnings; use DBI; my $file = 'f1'; my $dbh = DBI->connect("dbi:CSV:csv_eol=\n;csv_sep_char=|"); $dbh->{csv_tables}->{FOO}{file} = $file; $dbh->{csv_tables}->{FOO}{col_names} = [ qw/date id unk1 price unk2 qu +antity/ ]; my $r = $dbh->selectall_arrayref('SELECT * FROM FOO ORDER BY ID'); use Data::Dumper; print Dumper $r;
    At this point, you can do whatever you want with $r. But modifying the original file becomes unnecessary since you can always re-order the rows you're interested in by whatever field you want.

    I emphasise the tabular nature of your data because you're probably doing other things that SQL makes easy. And, you probably should also put it in a real db anyway, even if that real db is SQLite.

Re: Sort by specific column
by repellent (Priest) on Mar 05, 2009 at 17:17 UTC
    man sort

    Look for --field-separator and --key.
Re: Sort by specific column
by Random_Walk (Prior) on Mar 05, 2009 at 21:02 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-29 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found