# UNTESTED $max = ( sort { $a <=> $b } map { length } @files )[0]; # length of smallest filename - I hope. LIST: for $len ( 1..$max) ) { $s = substr($file[0],0,$len); last LIST unless scalar grep { /^$s/ } @files == @files; # precedence issue here? } print "prefix: '",substr($file[0],1,$len-1), "'\n";