Workshop in Computational Bioskills - sort_gene.pl

#!/usr/bin/perl -w

# Sort a list of genes by their length. 
# Receives an input file with gene name and length in each row 

die "Usage: sort_gene.pl < file >\n" if(@ARGV<1);

open (IN,"$ARGV[0]") || die "cannot open file\n";

while ($next = <IN>)
{
    chomp $next;
    ($id,$length) = split ("\t", $next);
    $id2length{$id} = $length;    
}

@keys = keys( %id2length );
@sorted = sort {$id2length{$a} <=> $id2length{$b} } @keys;

for ($k=0; $k <= $#sorted; $k++)
{
    print "gene: $sorted[$k]\t";
    print "length: $id2length{$sorted[$k]}\n";
}