Re: [MacPerl-AnyPerl] Sorting a list of alphanumeric strings
[email protected] (allan) Fri, 24 Aug 2001 23:33:56 +0200
| Newsgroups | perl.macperl.anyperl |
|---|---|
| Message-ID | <[email protected]> |
if the data really are that consistent you could try something like this
my @list = qw(exon1 exon5 exon12 exon30 exon2);
my %hash;
#loop thru list to get numbers to base sort on
foreach my $line (@list) {
$_ = $line;
my ($letters, $figures) = /([a-z]+(\d+))/i;
$hash{$letters} = $figures
}
my @sortedlist = sort by_numbers keys(%hash);
foreach my $line (@sortedlist) {
print $line, "\n"
}
#numeric sort
sub by_numbers {
return $hash{$a} <=> $hash{$b}
}
allan
Adam Witney wrote:
>
> Hi,
>
> I am trying to sort a list like this
>
> exon1
> exon5
> exon12
> exon30
> exon2
>
> Into ->
>
> exon1
> exon2
> exon5
> exon12
> exon30
>
> Any ideas on how to do this?
>
> Thanks
>
> adam