in reply to Re: Iterate over a perl nested hash data structure
in thread Iterate over a perl nested hash data structure

Sorry I didn't explain myself properly. I want to be able to iterate through the nested hashes and and depending on what the name of one of the hashes is I want to then print out all the key/value pairs in that particular hash and then output this data to another file

  • Comment on Re^2: Iterate over a perl nested hash data structure

Replies are listed 'Best First'.
Re^3: Iterate over a perl nested hash data structure
by BrowserUk (Patriarch) on Feb 20, 2013 at 06:54 UTC
    I want to be able to iterate through the nested hashes and and depending on what the name of one of the hashes is I want to then print out all the key/value pairs in that particular hash

    You don't need to "iterate" over a hash to find a particular value (subhash), you can go straight to it. That is the whole point of hashes.

    If you know the path to the hash you want to print, you can go straight there.

    Eg. To dump the key/value pairs in the hash at $HoHoH{ C }{ 2 }:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 100; my %HoHoH = map{ my $l1 = $_; $l1 => { map{ my $l2 = $_; $l2 => { map{ $_ => "$l1 $l2 $_" } 'a'..'d' } } 0 .. 4 }; } 'a'..'d'; pp \%HoHoH; ## print "key:$_ val: $HoHoH{ c }{ 2 }{ $_ }" for keys %{ $HoHoH{ c }{ 2 + } }; __END__ C:\test>junk30 { a => { "0" => { a => "a 0 a", b => "a 0 b", c => "a 0 c", d => "a 0 +d" }, 1 => { a => "a 1 a", b => "a 1 b", c => "a 1 c", d => "a 1 +d" }, 2 => { a => "a 2 a", b => "a 2 b", c => "a 2 c", d => "a 2 +d" }, 3 => { a => "a 3 a", b => "a 3 b", c => "a 3 c", d => "a 3 +d" }, 4 => { a => "a 4 a", b => "a 4 b", c => "a 4 c", d => "a 4 +d" }, }, b => { "0" => { a => "b 0 a", b => "b 0 b", c => "b 0 c", d => "b 0 +d" }, 1 => { a => "b 1 a", b => "b 1 b", c => "b 1 c", d => "b 1 +d" }, 2 => { a => "b 2 a", b => "b 2 b", c => "b 2 c", d => "b 2 +d" }, 3 => { a => "b 3 a", b => "b 3 b", c => "b 3 c", d => "b 3 +d" }, 4 => { a => "b 4 a", b => "b 4 b", c => "b 4 c", d => "b 4 +d" }, }, c => { "0" => { a => "c 0 a", b => "c 0 b", c => "c 0 c", d => "c 0 +d" }, 1 => { a => "c 1 a", b => "c 1 b", c => "c 1 c", d => "c 1 +d" }, 2 => { a => "c 2 a", b => "c 2 b", c => "c 2 c", d => "c 2 +d" }, 3 => { a => "c 3 a", b => "c 3 b", c => "c 3 c", d => "c 3 +d" }, 4 => { a => "c 4 a", b => "c 4 b", c => "c 4 c", d => "c 4 +d" }, }, d => { "0" => { a => "d 0 a", b => "d 0 b", c => "d 0 c", d => "d 0 +d" }, 1 => { a => "d 1 a", b => "d 1 b", c => "d 1 c", d => "d 1 +d" }, 2 => { a => "d 2 a", b => "d 2 b", c => "d 2 c", d => "d 2 +d" }, 3 => { a => "d 3 a", b => "d 3 b", c => "d 3 c", d => "d 3 +d" }, 4 => { a => "d 4 a", b => "d 4 b", c => "d 4 c", d => "d 4 +d" }, }, } key:c val: c 2 c key:a val: c 2 a key:b val: c 2 b key:d val: c 2 d

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.