#/usr/bin/perl -w use strict; my %sortOrder; # replace "file1" with the filename of the first file my $currentOrder=0; open(FILE1,") { chomp; # associate the current word with its place in the sort order $sortOrder{$_}=$currentOrder++; } close(FILE1); #### GAVARC|10.3.|Tahkoluoto|1m SOMMOL|10.3.|Tahkoluoto|7m GAVSTE|7.4.|Preiviiki|1p GAVARC|7.4.|Preiviiki|2p SOMMOL|16.3.|Kallo|6m #### my @data; open(FILE2,") { chomp; my ($keyField)=split /\|/,$_; die "Don't have a sort key for $keyField" unless exists $sortOrder{$keyField}; # push in a reference to an array where the first # element is the sort order. This is the Scwartzian # transform sorting method push @data,[$sortOrder{$keyField},$_]; } close(FILE2); my @sorted=map {pop @$_} sort {$a->[0] <=> $b->[0]} @data; # I'm assuming you want the | delimiters in the output? print join "\n", @sorted;