my %found; my %notfound; @notfound{1..12} = (); my $re = join "|", keys %notfound; for my $key (keys %test) { if ($key =~ m/\.0?($re)\./) { my $month = $1 + 0; $found{$month} ||= 1; delete $notfound{$month}; $re = join "|", keys %notfound; # wenn alle gefunden, braucht man gar nicht mehr suchen last unless keys %notfound; } } say for keys %found;