my @candidates = grep { /^$mask/ig } glob('*'); my @sorted = map { $_->[1] } sort { $b->[0] <=> $a->[0] } map { [ (stat)[9], $_ ] } @candidates; print "Your file is: ", $sorted[$position-1], $/ if defined $sorted[$position-1];