#!/usr/bin/perl -w use strict; my @km_testlist = qw( 120 500 300 400); my @LoH; #this will be a list of references to hash tables #now of course this just shows how to make and #sort this type of structure. #the stuff in this loop goes into your line process loop.. foreach my $thiskm (@km_testlist) #building a test structure { my %table = (); #don't need the =() but makes purpose very clear. $table{'val1'}="something"; #this data doesn't matter $table{'val2'}="aaaa"; $table{'km'}=$thiskm; push(@LoH,\%table); #pushes a ref to table hash onto @LoH } @LoH = sort { my $a_km = $a->{'km'}; #don't need the $a or $b_km temp my $b_km = $b->{'km'}; #trying to be clear for you $a_km <=> $b_km }@LoH; #so now instead of using the $vars, from each line, you use #the values in the hash, instead of 'val1', I would give the #sort of name you are already using. foreach my $tab_hashref (@LoH) { print "$tab_hashref->{'km'} $tab_hashref->{'val1'} $tab_hashref->{'val2'}\n"; } #prints: #120 something aaaa #300 something aaaa #400 something aaaa #500 something aaaa