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:

$VAR1 = { 'component1' => { 'name' => 'command4', 'id' => '004' }, 'component2' => { 'name' => 'command8', 'id' => '014' } };
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:
%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 keeps all your data intact. Next, you want to retrieve all the key/value pairs. Something 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"; } }
This will produce output like this:
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])
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.


In reply to Re: how to dereference for the following hash of hash of arrays by Tanktalus
in thread how to dereference for the following hash of hash of arrays by srins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.