[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] perl



On Apr 20, 2012, at 8:36 AM, Klinkebiel, David L wrote:
> I know you are getting tired of me and I apologize. I need a perl that can sort the file by first chromosome then the start location if you have time. 

Ha! Not at all. So, sometimes you can get away with

   sort -n --key 1,2 inputfile.txt 

But in this case you have a mix of non-numeric and numeric keys you want to sort on so sort is being dumb. So here's a little Perl program:

-----------
use strict;
use 5.10.0;

my @lines;
print scalar(<>);  # header
while (<>) {
   chomp;
   push @lines, [ split /\t/ ];
}

foreach my $arrayref (sort by_chr_then_pos @lines) {
   say join "\t", @$arrayref;
}

sub by_chr_then_pos {
   $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1]
}
-----------

If that's sort.pl then you can run

   perl sort.pl inputfile.txt

and should get what you expect out.

HTH,

j