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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.