use strict; use File::Find; my %temp; find( sub { m/^(.+)\.(mrg|did)/ and push(@{$temp{"$1.did"}}, $2) }, '.' ); my @files = grep { @{ $temp{$_} } == 1 and $temp{$_}->[0] eq 'did' } sort keys %temp; print "@files";