my @baselist = qw ( alpha beta etc ); # or from wherever my %baselist = (); $baselist{ $baselist[$_] } = $_ for ( 0..$#baselist ); # find baselist in the order it appears in input my %rank = (); my $rank = 0; while(<>) chomp; defined( $baselist( $_ ) ) or next; if ( defined( $rank{ $_ } ) { warn "duplicate entry at line $.\n"; } else { $rank{ $_ } = ++$rank; } } # print order actually found print "$_\n" for sort { $rank{$a} <=> $rank{$b} } keys %rank;