# Snippet 1 - a stepping stone use strict; use warnings; use Data::Dumper; # The phone book in an ordinary format, # as it might be read from a text file for example my $numbers = "Alice: 555 1234\n" . "Bob: 555 9876\n" . "Charlie: 555 2580"; # The hash. Still empty. my %phonebook = (); # Split $numbers on newlines, then iterate over the returned elements # Then get the name and the number from each element and store them in the hash. for my $line ( split(/\n/, $numbers) ) { my ($name,$number) = split(/:\s+/, $line); $phonebook{$name} = $number; } # Bob's a cool guy. I should call him. What's his number again? print "Bob's digits are $phonebook{Bob}\n\n"; # Heck, Alice, Bob, Charlie -- they're all pretty cool! print Dumper \%phonebook; #### Bob's digits are 555 9876 $VAR1 = { 'Bob' => '555 9876', 'Alice' => '555 1234', 'Charlie' => '555 2580' }; #### # Snippet 2a - another stepping stone use strict; use warnings; use Data::Dumper; # Create a normal hash. my %alice = ( home => "555 1234", work => "555 9876", mobile => "555 2580", ); # And another one. my %bob = ( home => "555 2345", work => "555 8765", mobile => "555 1357", ); my %phonebook = (); $phonebook{Alice} = \%alice; # Method 1: Store a direct reference to the %alice hash $phonebook{Bob} = { %bob }; # Method 2: Create an anonymous hash ref, with the contents of %bob $phonebook{Charlie} = { # Method 3: Create and store an anonymous hash ref directly home => "555 3456", work => "555 7654", mobile => "555 2468" }; # Alice's a cool lady. I want to call her at her home number! print "Alice's home number is $phonebook{Alice}->{home}\n"; # She's not there. Maybe she's with Charlie, let's call his cell phone. print "Charlie's mobile number is $phonebook{Charlie}->{mobile}\n\n"; # Gimme all their digits! print Dumper \%phonebook; #### Alice's home number is 555 1234 Charlie's mobile number is 555 2468 $VAR1 = { 'Bob' => { 'work' => '555 8765', 'home' => '555 2345', 'mobile' => '555 1357' }, 'Alice' => { 'work' => '555 9876', 'mobile' => '555 2580', 'home' => '555 1234' }, 'Charlie' => { 'work' => '555 7654', 'mobile' => '555 2468', 'home' => '555 3456' } }; #### # Snippet 2b - append to snippet 2a for (1..3) { printf "Alice and Bob: %s and %s\n", \%alice, {%bob} } #### Alice and Bob: HASH(0x328358) and HASH(0x380138) Alice and Bob: HASH(0x328358) and HASH(0x3801e0) Alice and Bob: HASH(0x328358) and HASH(0x380168) #### # Snippet 2c -- append after 2b delete $alice{home}; # Delete Alice's home number **from her original hash** delete $bob{home}; # Delete Bob's home number **from his orignal hash** too delete $phonebook{Charlie}; # Let's not clutter the output print Dumper \%phonebook; #### $VAR1 = { 'Bob' => { 'work' => '555 8765', 'home' => '555 2345', 'mobile' => '555 1357' }, 'Alice' => { 'work' => '555 9876', 'mobile' => '555 2580' } }; #### # Snippet 3 - your answer? use strict; use warnings; use Data::Dumper; my @primary = qw(foo-Alice bar-Bob); my @secondary = qw(p0-home p1-work p2-mobile); my @tertiary = qw(itemA-555abcd itemB-555zxyw); my %phonebook = (); for my $primary_element (@primary) { for my $secondary_element (@secondary) { # Similar to method 1 from snippet 2a: # a reference directly to @tertiary $phonebook{$primary_element}->{$secondary_element} = \@tertiary; # -- or -- # Similar to method 2 from snippet 2a: # a new anonymous array reference, containing the elements from @tertiary $phonebook{$primary_element}->{$secondary_element} = [ @tertiary ]; } } print Dumper \%phonebook; #### $VAR1 = { 'bar-Bob' => { 'p1-work' => [ 'itemA-555abcd', 'itemB-555zxyw' ], 'p0-home' => [ 'itemA-555abcd', 'itemB-555zxyw' ], 'p2-mobile' => [ 'itemA-555abcd', 'itemB-555zxyw' ] }, 'foo-Alice' => { 'p1-work' => [ 'itemA-555abcd', 'itemB-555zxyw' ], 'p0-home' => [ 'itemA-555abcd', 'itemB-555zxyw' ], 'p2-mobile' => [ 'itemA-555abcd', 'itemB-555zxyw' ] } };