use strict; use warnings; my @rows; while ( ) { 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