Listing from a multidimensional hash in Perl

In Perl programming (book), I read that I can create a dictionary in which records store an array as follows:

$wife{"Jacob"} = ["Leah", "Rachel", "Bilhah", "Zilpah"]; 

Say I want to grab the contents of $wife{"Jacob"} in the list. How can i do this?

If I try:

 $key = "Jacob"; say $wife{$key}; 

I get:

 ARRAY (0x56d5df8) 

which makes me believe that I get the link, not the actual list.

+1
source share
2 answers

Cm

for information on using complex data structures and references.

In fact, a hash can only have scalars as values, but links are scalars. Therefore, you save arrayref inside the hash and must dereference it in the array.

To dereference a link, use the syntax @{...} .

 say @{$wife{Jacob}}; 

or

 say "@{$wife{Jacob}}"; # print elements with spaces in between 
+4
source

I think by this time you should know that $ refers to a scalar and @ refers to an array.

since you yourself said that the value for this key is an array, then you should

 say @wife{$key}; 

instead

 say $wife{$key}; 
+1
source

Source: https://habr.com/ru/post/1434499/


All Articles