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: Intermediate:
sorting the contents of a file

 



depk
New User

Aug 1, 2011, 9:13 AM

Post #1 of 7 (1992 views)
sorting the contents of a file Can't Post

HI
i have a ".csv" file and i need to sort the content of this file in perl, sorting has to be done based on the column number (for example if i say 4 th column then the contents of the file has to be sorted based on the 4th column )..... could you please help me with it
is it possible to sort the contents of any file according to a particular column
[:)]


BillKSmith
Veteran

Aug 1, 2011, 4:05 PM

Post #2 of 7 (1979 views)
Re: [depk] sorting the contents of a file [In reply to] Can't Post

Use the Schwartzian transformation (refer: perldoc -q "How do I sort an array")
Good Luck,
Bill


depk
New User

Aug 1, 2011, 9:55 PM

Post #3 of 7 (1949 views)
Re: [BillKSmith] sorting the contents of a file [In reply to] Can't Post

Hi,

could you help me out in writing a program to sort contents/data of a file based on a particular column, the data has both alphabet and numeric.

Please help me as i am new to perl.


BillKSmith
Veteran

Aug 2, 2011, 4:20 AM

Post #4 of 7 (1935 views)
Re: [depk] sorting the contents of a file [In reply to] Can't Post

Show us what you have done so far. Did you read the FAQ I referred to? What did you not understand?
Good Luck,
Bill


depk
New User

Aug 2, 2011, 10:22 PM

Post #5 of 7 (1878 views)
Re: [BillKSmith] sorting the contents of a file [In reply to] Can't Post

I am opening the file and sorting based on the column specified
Here is the part of code...

open(F1, "C:\\txt.CSV") or die "Cannot open the file";

my @sorted = map {$_->[0]}
sort { $a->[$col] <=> $b->[$col] }
map {chomp;[$_,split(/,/)]} <F1>;

print "$_\n" for @sorted;
close (F1);
where $col is the column number......
here i'm sorting for a CSV file, I want to know how sorting can be done for a text file or any other type of file.

And also, I want to know whether there is any direct sort command in perl like we have in unix
"sort -k 2 filename" where it sorts based on the 2nd column


BillKSmith
Veteran

Aug 3, 2011, 11:34 AM

Post #6 of 7 (1850 views)
Re: [depk] sorting the contents of a file [In reply to] Can't Post

You got the right idea. Note in your algorithm, refer to the fourth column with $col=4. There are variations to save time and/or memory. (e.g. Your temp array could consist of just two columns, the column you wish to sort and either an index or reference into the original array.)



Perl does not have a unix like "column sort" built-in. I have not looked, but I would bet there is a module on CPAN to do what you want. If not, package your sort as module yourself.
Good Luck,
Bill


depk
New User

Aug 8, 2011, 9:49 PM

Post #7 of 7 (1795 views)
Re: [BillKSmith] sorting the contents of a file [In reply to] Can't Post

Ya I went Through CPAN there were few modules which are help full but i'll build my own... Lets see how it comes out.... Thank You

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives