while ( my ($key, $value) = each %ids ) { for my $item (@names) { if ($item =~ /$key/) { push @groups, $item; } } }