use strict; use warnings; my %names; /^([^.]+)\.(\d{3})$/ and $names{$1}[$2] = $_ while ; print $names{$_}[-1] for sort keys %names; __DATA__ file.001 file.003 not.good.001 file.002 file.10 one.004 two.001 two.003 two.five one.002 one.001 one.0039 two.002 .005