Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Sorting big text lists

by krujos (Curate)
on Jul 30, 2002 at 18:15 UTC ( #186286=note: print w/ replies, xml ) Need Help??


in reply to Sorting big text lists

Perl has a sort built in to do things like this. You will also want to checkout split. Depending on how the files are set up (delimiter) you are going to want to split on that and sort by the correct element. An example (untested) for something like this would be the following. We are splitting on commas.

#!/usr/bin/perl -w use strict; #always use strict my %sortKey; open (INFILE, "filename"); #open the file while <INFILE> { #iterate through the file line by line my @tmp = split /,/; #split the file on the comma #for the sake of the example we will say that the first #element of the file is what you want to sort on #so we will make it the key to our hash $sortKey{$tmp[0]}=$_; } open (OUTFILE ">outfile"); #this will take the keys, shove them in an array, sort #them and set $key equal to the current sorted key foreach my $key (sort(keys(%sortKey))) { print OUTFILE " %sortKey{$key}; }

Hopefully this will give you a pretty good idea how to do the first part, and enough info on how to do the second part. Feel free to /msg me if you have questions.
UPDATE: fixed a syntax error


Comment on Re: Sorting big text lists
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://186286]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-08-23 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls