Sure, it'd be:
map { $val = $hash{$_} } @strings;
That is, each value of @strings
is set in $_
in turn (instead of $str
as in your foreach).
Of course, this doesn't do much, since you're not doing anything with the value of $val
in your loop, and we aren't capturing the list returned by map
.
If you're just trying to generate a list of values, that'd be:
@values = map { $hash{$_} } @strings;
But it's more concise to use a hash slice:
@values = @hash{@strings};
EDIT: As pointed out in the comments, if it's possible that @strings
contains values that aren't keys in your hash, then @values
will get undefs in those positions. If that's not what you want, see Hynek's answer for a solution.