in reply to Sorting an array of strings by number
You can do this while reading your data:my $padded=sprintf("%08.3f",39.452);
while (<>){ split /:/; $_[0]=sprintf("%08.3f", $_[0]); push @data, join ":", @_; }
. You can write a sub for the extraction:sort{ $a <=> $b} @numbers;
sort{extract( $a ) <=> extract( $b )} @lines; sub extract{ $a = shift; split /:/, $a; $_[0]; }
my %hash; while(<>){ my ($key, $data) = split( /:/, $_, 2 ); $hash{ $key } = $data; } print "Sorted!:\n\n", join "|", sort {$a <=> $b} keys %hash;
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};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting an array of strings by number
by Albannach (Monsignor) on Oct 01, 2001 at 17:27 UTC |