use strict; use warnings; my @rows; while ( <DATA> ) { chomp; #split row and put in anon array pushed into @rows push @rows, [ split /\|/ ]; } #sort @rows (an Array of Arrays) by column; #@rows looks something like: (ARRAY(0x...),ARRAY(0x...),..) #where ARRAY(0x...) is just a reference to another array. my @sorted = sort { $a->[2] <=> $b->[2] } @rows; #since @sorted is now an array of Arrays sorted #we need to dereference what's in it to print it. foreach my $array_reference ( @sorted ){ #lets join the array pointed to in the array reference # with a "|" # @ dereferences the array #and print print join("|",@$array_reference),$/; } __DATA__ My|Text|5|ACGT My|Text|2|ACGT My|Text|4|ACGT My|Text|3|ACGT My|Text|1|ACGT
As mentioned in the CB you might want to have a look at perlreftut, perldoc perllol, and perldoc perldsc for more on dereferencing (which you had to do to sort in the first place). (there is also references quick reference and intro to references on this site you can look at for more info). Hope this helps.
-enlil
In reply to Re: how to print sorted data
by Enlil
in thread how to print sorted data
by ioana
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |