How does one create a new hash using an array of keys on an existing hash?
my %pets_all = (
'rover' => 'dog',
'fluffy' => 'cat',
'squeeky' => 'mouse'
);
my @alive = ('rover', 'fluffy');
#this does not work, but hopefully you get the idea.
my %pets_alive = %pets_all{@alive};
my %pets_alive = %pets_all{@alive}; is called a key/value hash slice, and it actually does work since recently-released 5.20.
Before 5.20, you have a couple of alternatives:
my %pets_alive; @pets_alive{@alive} = @pets_all{@alive}; (Hash slices)
my %pets_alive = map { $_ => $pets_all{$_} } @alive;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With