The sort function takes an optional subroutine which is where you can add
you logic for the sort. The subroutine is passed two items from the list at
a time and needs to return -1 is the first item comes first, 1 if the second
item comes first, or 0 if the values are equal. The vales are passed to
your routine as $a and $b;
Something like this should do it:
# Untested, but I hope this helps
# Open $file1 and read lines into the array
open FILE1, $file1;
my @file1 = <FILE1>;
close FILE1;
# Sort it!
# The sub: split $a, $split $b, use "cmp" to compare the string values
@file2 = sort {@a=split('|',$a) ;@b=split('|',$b); $a[11] cmp $b[11]}
(@file1);
# Open $file2 and write the sorted data to it
open FILE2, ">$file2";
print FILE2 join('', @file2);
close FILE2;
Notes:
"cmp" does an ASCII comparison, so "Abe" is less than "abe". If you want to
to be equal use lc() when you "cmp" (compare) them. Also if you want a
numeric compare instead of a string compare, use "<=>" instead of "cmp".
Rob
-----Original Message-----
From: Mayank [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 28, 2002 9:56 AM
To: PERL
Cc: [EMAIL PROTECTED]
Subject: sort
Hi all
i'm a newbie.....so this might appear to be an easy one!
How can i have an equivalent of following UNIX command in PERL?
sort -t "|" -k 11,11 $file1 > $file2
Which means sort file "$file1" on the basis of 11th field with "|" as
the delimiter
Thanks in advance
Regards
Mayank
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]