Question

I want to sort a tabular file based on the 3rd column. I have this code so far but it doesn't seem to work.

#!/usr/local/bin/perl

use warnings;
use strict;

my (@row, @rows);
while (<DATA>) {
    chomp;
    @row = split ("\t",$_);
    push (@rows, \@row);
}

my @sorted = sort { $b->[2] <=> $a->[2] } @rows;

foreach (@sorted) {
    print join ("\t", @$_),"\n";
}

__DATA__
A   3277    6   1   0   0   0
B   2136    1   0   0   0   0
C   2653    0   0   0   0   0
D   11  3541    0   0   0   0
E   3258    1628    0   0   0   2

is always printing:

E   3258    1628    0   0   0   2
E   3258    1628    0   0   0   2
E   3258    1628    0   0   0   2
E   3258    1628    0   0   0   2
E   3258    1628    0   0   0   2

where I would like the output to be like this:

D   11  3541    0   0   0   0
E   3258    1628    0   0   0   2
A   3277    6   1   0   0   0
B   2136    1   0   0   0   0
C   2653    0   0   0   0   0

Any help about why is this happening and what can be done to output the correct form? Thanks

Was it helpful?

Solution

The sort is working fine, the error is in reading the data. You're overwriting the contents of @row and pushing multiple references to it into @rows. Instead, create a new @row for each line of data:

my @rows;
while (<DATA>) {
    chomp;
    my @row = split "\t";
    push (@rows, \@row);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top