in reply to how to dereference for the following hash of hash of arrays
srins, the structure you have is actually a hash of hashes. You should try Data::Dumper to see a structure's layout whenever you're having trouble grasping it. In this case, the layout is like this:
Now you're probably asking what the heck happened to the rest of your data. It got overwritten - a hash can only have a key once, and you've got 4 'name' keys and 4 'id' keys - only the last one is kept. What you probably want is a hash of arrays of hashes:$VAR1 = { 'component1' => { 'name' => 'command4', 'id' => '004' }, 'component2' => { 'name' => 'command8', 'id' => '014' } };
This keeps all your data intact. Next, you want to retrieve all the key/value pairs. Something like this:%component_tree = ( component1 => [ { name => 'command1', id => '001' }, { name => 'command2', id => '002' }, { name => 'command3', id => '003' }, { name => 'command4', id => '004' }, ], component2 => [ { name => 'command5', id => '011' }, { name => 'command6', id => '012' }, { name => 'command7', id => '013' }, { name => 'command8', id => '014' }, ], );
This will produce output like this:for my $comp (keys %component_tree) { print "Checking $comp...\n"; for my $data (@{$component_tree{$comp}}) { print " Found ", $data->{name}, " (id [", $data->{id}, "])\ +n"; } }
Note that the order of "Checking componentn..." is going to be random. It was just luck that this time they ended up in the same order that we inserted them. Count on that not being true as the hash grows. However, the order of commands is not luck - that is guaranteed since we're using an array.Checking component1... Found command1 (id [001]) Found command2 (id [002]) Found command3 (id [003]) Found command4 (id [004]) Checking component2... Found command5 (id [011]) Found command6 (id [012]) Found command7 (id [013]) Found command8 (id [014])
|
|---|