@files = qw(model4run1 model5run1); #@files = qw(model4run1 model2run1 model4run2 model1run1); my $first = shift(@files); my $and = $first; my $or = $first; foreach (@files) { $and &= $_; $or |= $_; } my $combined = $and ^ $or; $combined =~ s/[^\x00].*//; my $prefix = substr($first, 0, length($combined)); print qq{Prefix is "$prefix"};