use List::Util qw( first ); my $picture = first { /$match/ } sort @pictures; #### my $picture = do { my @candidate = sort @pictures; shift @candidate while @candidate and $candidate[0] !~ /$match/; shift @candidate; };