Is there a way to get a sub-hash? Do I need to use a hash slice?
For example:
%hash = ( a => 1, b => 2, c => 3 );
I want only
%hash = ( a => 1, b => 2 );
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hash slices return the values associated with a list of keys. To get a hash slice you change the sigil to @ and provide a list of keys (in this case
"a"and"b"):Often you can use a quote word operator to produce the list:
You can also assign to a hash slice, so if you want a new hash that contains a subset of another hash you can say
Many people will use a map instead of hash slices:
Starting with Perl 5.20.0, you can get the keys and the values in one step if you use the % sigil instead of the @ sigil: