my @temp; for my $h (@hosts) { if ($h =~ m/^\d{2}\z/) { for my $short (@shorts) { push @temp, "$short.area$h"; } } else { push @temp, $h; } } @hosts = @temp;