How can I create hash hashes in Perl?

I am new to Perl. I need to define a data structure in Perl that looks like this:

city 1 -> street 1 - [ name , no of house , senior people ] street 2 - [ name , no of house , senior people ] city 2 -> street 1 - [ name , no of house , senior people ] street 2 - [ name , no of house , senior people ] 

How can I understand that?

+4
source share
5 answers

Here is another example using a hash link:

 my $data = { city1 => { street1 => ['name', 'house no', 'senior people'], street2 => ['name','house no','senior people'], }, city2 => { street1 => etc... ... } }; 

Then you can access the data as follows:

 $data->{'city1'}{'street1'}[0]; 

Or:

 my @street_data = @{$data->{'city1'}{'street1'}}; print @street_data; 
+5
source

I found an answer like

 my %city ; $city{$c_name}{$street} = [ $name , $no_house , $senior]; 

i can generate in this way

+4
source

The Perl Data Structures cookbook , perldsc , can help. It has examples showing how to create common data structures.

+1
source
 my %city ; 

If you want to click

 push( @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior] ); 

(0r)

 push @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior]; 
0
source

You can read my short tutorial in this answer. In short, you can put a hash reference in a value.

 %hash = ( name => 'value' ); %hash_of_hash = ( name => \%hash ); #OR $hash_of_hash{ name } = \%hash; # NOTICE: {} for hash, and [] for array %hash2 = ( of_hash => { of_array => [1,2,3] } ); # ---^ ---^ $hash2{ of_hash }{ of_array }[ 2 ]; # value is '3' # ^-- lack of -> because declared by % and () # same but with hash reference # NOTICE: { } when declare # NOTICE: -> when access $hash_ref = { of_hash => { of_array => [1,2,3] } }; # ---^ $hash_ref->{ of_hash }{ of_array }[ 2 ]; # value is '3' # ---^ 
0
source

All Articles