use strict; use IO::File; # load main file my %main_data; my $f = new IO::File "mainfile.txt", "r" or die "Can't open main db: $!"; while(<$f>) { chomp; my @res = split /\t/; $main_data{$res[2]}{data} = \@res; # 3rd field is the ID number } undef $f; # load sort reference file my $f = new IO::File "sort.txt", "r" or die "Can't open sort.txt: $!"; while (<$f>) { chomp; my ($id, $total) = split /\t/; if (exists $main_data{$id}) { $main_data{$id}{total} = $total; # insert additional values } } # sort the main db in descending order my @sorted_id = sort { $main_data{$b}{total} <=> $main_data{$a}{total} } keys %main_data; # print sorted main db print "@{$main_data{$_}{data}}\n" foreach @sorted_id;