chtaylo2 has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I have the following hash being created. I'm looping through various $sub_alias and $topic_sub so I know the value of those. My question is how do I access the value of $id and the URL / TITLE etc...
$data{$sub_alias}{$topic_sub}{$id}{ 'URL' } = 'my_url'; $data{$sub_alias}{$topic_sub}{$id}{ 'TITLE' } = 'mytitle'; $data{$sub_alias}{$topic_sub}{$id}{ 'CONTENTTYPE' } = 'my_type'; $data{$sub_alias}{$topic_sub}{$id}{ 'SUBCATEGORY' } = 'my_sub'; $data{$sub_alias}{$topic_sub}{$id}{ 'TOPIC_DESCRIPTION' } = 'my_desc'; $data{$sub_alias}{$topic_sub}{$id}{ 'RELATED_TOPICS' } = 'my topic';
Something like this maybe?
foreach $tmp_sa (@foo) { foreach $t (@bar)..... { foreach ( keys %{$data->{$tmp_sa}{$t}} ) { print "$data->{$tmp_sa}{$t}{$_}{'URL'}\n"; } } }
Thank you!

Replies are listed 'Best First'.
Re: Perl Hashes / Accessing data
by locked_user sundialsvc4 (Abbot) on Jul 30, 2010 at 00:06 UTC

    Another nifty trick is to use the each() function, which iterates directly over the keys in a hash without making a separate copy.   May not be applicable in your case, but always worth noting.   (It once made a huge difference in a program that was working with a “hash” that was actually tied to a massive Berkeley-DB file...)

      Thank you both. Worked great!
Re: Perl Hashes / Accessing data
by morgon (Priest) on Jul 29, 2010 at 23:02 UTC
    $data{$sub_alias}{$topic_sub}{$id}{ 'URL' } = 'my_url';
    creates a %data-hash (not a $data reference to a hash - I hope you know what I mean).

    So do get to get to the $id-values you do need to get rid of the "->":

    foreach $tmp_sa (@foo) { foreach $t (@bar)..... { foreach ( keys %{$data{$tmp_sa}{$t}} ) { print "$data{$tmp_sa}{$t}{$_}{'URL'}\n"; } }