CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [yellowman] Problems with sorting a record: Edit Log

Thaumaturge / Moderator

Mar 1, 2006, 5:31 AM

Views: 7384
Re: [yellowman] Problems with sorting a record

You really don't need Perl for something like this. Assuming you have access to Unix box (or a Windows box with Cygwin installed) you can do this:

sort -k 4 < input_file > output_file

But if you insist on Perl, then you can do something like this:

print sort my_sort <STDIN>; 

sub my_sort {
my @a = split /\s+/, $a;
my @b = split /\s+/, $b;

return $a[3] cmp $b[3];

Like my first example, it's implemented as a filter. That is it reads data from standard input and writes it to standard output. Assuming the code is in a file called "my_sort", you would call it like this:

my_sort < input_file > output_file

You should read the documentation for sort.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks

(This post was edited by davorg on Mar 1, 2006, 7:19 AM)

Edit Log:
Post edited by davorg (Thaumaturge) on Mar 1, 2006, 7:19 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