Wednesday, December 29, 2010

Alphanumeric sort in Perl

Method 1:

@input = (exon1, exon5, exon12, exon30, exon2);
my @sorted = sort { substr($a, 4) <=> substr($b, 4) } @input;
print "@sorted";


Method 2:

use strict;
use warnings;
use Data::Dumper;

my $fh = \*DATA;

my %lines;
while (my $line = <$fh>) {
    chomp($line);
    $lines{$_}++ for split /,/, $line;
}

my @sorted_array = sort keys %lines;
print Dumper \@sorted_array;


__DATA__
A2B12,A8B15
A3B27
A5B14,A8B15,A5B18

No comments: