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: Perl Programming Help: Beginner: Re: [yellowman] Problems with sorting a record: Edit Log



davorg
Thaumaturge / Moderator

Mar 1, 2006, 5:31 AM


Views: 3139
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:


Code
sort -k 4 < input_file > output_file


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


Code
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:


Code
my_sort < input_file > output_file


You should read the documentation for sort.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
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