There is more than one path to take here:
-
Zero-pad your numbers, so generate them
with
my $padded=sprintf("%08.3f",39.452);
You can do this while reading your data:
while (<>){
split /:/;
$_[0]=sprintf("%08.3f", $_[0]);
push @data, join ":", @_;
}
- Or you can sort numerically. This requires you to extract
the numbers and sort on them with <=> (see perlop), like
sort{ $a <=> $b} @numbers;
. You can write a sub
for the extraction:
sort{extract( $a ) <=> extract( $b )} @lines;
sub extract{
$a = shift;
split /:/, $a;
$_[0];
}
- It's better to extract your numbers, and use them as
keys in a hash, and work with a list of sorted keys:
my %hash;
while(<>){
my ($key, $data) = split( /:/, $_, 2 );
$hash{ $key } = $data;
}
print "Sorted!:\n\n", join "|", sort {$a <=> $b} keys %hash;
- .... many more paths to come up with
HTH,
Jeroen
"We are not alone"(FZ)
(code untested, you'll get the idea)
Update
Albannach is right of course. Still, if you have quite some
data, a hash-lookup is more efficient. And more scalable,
as you can tie your hash onto a database,
e.g. To prevent that double-count problem, add a check in for
the key, if a double, add a counter or something.
$key .= ':001' if defined $hash{$key};
$key++ while defined $hash{$key};