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

Hi all,
%HOH = ( Pegname1 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' } section3 => { val3 => 'Value 3' } Pegname2 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' } section3 => { val3 => 'Value 3' } )

Question 1: How to travel in the hash which is shown in the example ?

Pegname2 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' } section3 => { val3 => 'Value 3' }
Question 2: How to add the above key in HOH :-

Replies are listed 'Best First'.
Re: hash inside hash inside hash
by wfsp (Abbot) on Apr 21, 2006 at 06:17 UTC
    Here's one way. Note there were some commas missing from your data structure.

    Have a look at perldsc - complex data structures

    #!/usr/bin/perl use strict; use warnings; my %HOH = ( Pegname1 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' }, section3 => { val3 => 'Value 3' }, }, Pegname2 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' }, section3 => { val3 => 'Value 3' }, } ); # question 2 - add a new value $HOH{Pegname3}{section1}{val1} = 'Value 1'; # question 2 - add a new record my $record = { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' }, section3 => { val3 => 'Value 3' }, }; $HOH{Pegname3} = $record; # question 1 - traverse the data structure for my $peg (sort keys %HOH){ print "$peg\n"; for my $section (sort keys %{$HOH{$peg}}){ print "\t$section\n"; for my $val (sort keys %{$HOH{$peg}{$section}}){ print "\t\t$val\n"; } } } __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl Pegname1 section1 val1 section2 val2 section3 val3 Pegname2 section1 val1 section2 val2 section3 val3 Pegname3 section1 val1 section2 val2 section3 val3 > Terminated with exit code 0.
    Update: Added 'add new record'
    Update 2: Forgot to update ouput

Re: hash inside hash inside hash
by bowei_99 (Friar) on Apr 21, 2006 at 06:08 UTC
    Could you clarify what you mean by 'travel'? Like, you want to print out all the keys and their values? Or, do you want just 'Value 1', 'Value 2', 'Value 3'? In general, an easy way to display hash contents is to Data::Dumper.
    use Data::Dumper; #define your hash here %HOH = ( Pegname1 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' } section3 => { val3 => 'Value 3' } Pegname2 => { section1 => { val1 => 'Value 1' }, section2 => { val2 => 'Value 2' } section3 => { val3 => 'Value 3' } ) #print its contents print Dumper(\%HOH);

    For your second question, I'd suggest you look at Programming Perl, and check out the chapter on complex lists (i.e. arrays of arrays, hashes of hashes, hashes of arrays, etc.).

    -- Burvil

Re: hash inside hash inside hash
by nevyn (Monk) on Apr 21, 2006 at 21:34 UTC

    The big thing you are missing is that you don't have a hash inside a hash, you have a hash reference, inside a hash. That is why you have to use {} instead of (). The rest of your questions should be answered by the perlref documentation.

    --
    James Antill