[plug] [Perl] sorign arrays based on scalar value
Anthony J. Breeds-Taurima
tony at cantech.net.au
Fri Nov 15 11:29:11 WST 2002
On Fri, 15 Nov 2002, Patrick Tehvand wrote:
> 'ello all,
> I sent this to the SLPWA list by mistake...
>
> I've got another perl question,
> I would like to sort in decending nemerical order based on $id
> This is what I have.
>
> sub resort() {
> #Sort everthing out into numerical order
> my @final;
> my $inputfile=$_[0];
> open (THING,"<$inputfile");
> my @sortinput=<THING>;
> close THING;
> for $input(@sortinput) {
> chop ($input);
> ($id,$title,$words)=split(/\|/,$input);
>
> @final = ____some sorting stuff___
>
> open (RESORT,">$inputfile");
> for $line(@final) {
> print RESORT "$line";
> }
> close RESORT;
> return 0;
> }
> }
use a Hash;
sub resort($$) {
my $inputfile = shift; #Get function args.
my $outputfile = shift;
#pre-decalre vars.
my %allData;
#Open the input file
open(IN, "<$inputfile") or die;
while(<IN>) {
# extract data into individual vars and store in a hash.
chop;
my ($id,$title,$words)=split(/\|/,$input);
$allData{$id}{title}=$title;
$allData{$id}{words}=$words;
}
close(IN);
open(OUT,">$outputfile") or die;
#reverse sort by id [I'm assuming it's numeric] and output.
foreach my $id (sort {$b <=> $a} keys %allData) {
print join('|',$id,$allData{$id}{title},$allData{$id}{words}),
"\n";
}
close(OUT);
retirn 0;
}
It's not the only way to do it, and if your dat set is large then there are
better ways but, this should demo the perl sort function.
#include <http://thor.cantech.net.au/~tony/stddisclaimer.h>
Yours Tony
Jan 22-25 2003 Linux.Conf.AU http://linux.conf.au/
The Australian Linux Technical Conference!
More information about the plug
mailing list