in reply to Re^2: map and grep (but for hashes)
in thread map and grep (but for hashes)

>> @new_keys = grep { $hash{$_} == $condition } keys %hash; >> @new_hash{@new_keys} = @hash{@new_keys};

OK, sorry I missed this second line. This is how you put everything together. Fairly terse. I'd imagine map is a little less terse.

So I think this argues for a little function. I just wanted to make sure that there wasn't any really terse form that eliminated the need for a function.

Replies are listed 'Best First'.
Re^4: map and grep (but for hashes)
by DStaal (Chaplain) on Jan 30, 2009 at 20:39 UTC

    Oh, there probably is. I could one-line the above with not much work if I really wanted to. It would just be ugly.

    Hmm, thinking about map... I'm not completely sure this would work, but I think it would: (I'm assuming you want to modify the values.)

    @new_hash{keys %hash} = map { mod_func($hash{$_}) } keys %hash;

    (This assumes the two invocations of 'keys' will be stable, which I'm not sure you can rely on.)