Re: Sorting a list of alphanumeric strings
[email protected] ("Steve Driver") Fri, 24 Aug 2001 18:22:36 -0400
| Newsgroups | perl.macperl.anyperl |
|---|---|
| Message-ID | <[email protected]> |
It really helps to know as much about what is there.
Do you know that everything will be 4 letters ("exon") followed by an
unknown number of digits?
Lets assume you have a simple four letter word followed by an unknown
number. No, not that four letter word and two digit number!
ok, seriously, we'll also assume no two entries have the same number, and if
they did you'd want to delete repeats. This makes things a lot easier.
#! perl
open FILE, 'file.txt';
@list = <FILE>; # get list into array or by some other means
# keep the line breaks if you can
for $i (0..$#list) {
$list[$i] =~ /(....)(\d*)/
$sortedlist[$2] = $list[$i]; # create each element of the
# new list
}
print @sortedlist;
# or as a subroutine taking the list as arguments:
sub sortmylist {
my ($i,@sortedlist);
for $i (0..$#_) {
$_[$i] =~ /(....)(\d*)/
$sortedlist[$2] = $_[$i];
}
@sortedlist;
}
The nice thing is if you have gaps in the array (e.g. elements 2,3,4 exist
but 5-83 don't) it really won't matter.
Hope this helps!
I'm sure it can get really complicated if you have many different combos of
letters at the beginning. But if you can separate those out into separate
lists then run the subroutine over each of them, that'll do it.
--Steve
----- Original Message -----
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