in reply to Iterate over a perl nested hash data structure

I am able to decode this data and print the data structure using Data::Dumper. The problem I am having is that I want to be able to iterate over this data structure which is a hash of hashes and print out the all the key/value pairs

You say you can data Dump the structure; and then say you want to print everything out; which is exactly what Data::Dumper does.

So, what do you want to do different to the way Data::Dumper does it?


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.
  • Comment on Re: Iterate over a perl nested hash data structure

Replies are listed 'Best First'.
Re^2: Iterate over a perl nested hash data structure
by NewLondonPerl1 (Acolyte) on Feb 20, 2013 at 06:00 UTC

    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

      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.
Re^2: Iterate over a perl nested hash data structure
by NewLondonPerl1 (Acolyte) on Feb 20, 2013 at 06:20 UTC
    This is what data::dumper prints out. I want to be able to iterate over this. So for example if $VAR2 = 'dev', then read 'nfsmount' and then depending on whether the next level down equals 'bigstor_nfs'/'mbig_nfs'/'build_nfs'/'cc_nfs'/'home_nfs' I want to then be able to iterate over those individual hashes and extract particular key/value pairs and output to another file:
    $VAR1 = 'tag'; $VAR2 = { 'dev' => [ { 'nfsmount' => { 'bigstor_nfs' => { 'bigstor17_m +pt' => { + 'filer_device' => { + 'ny_loc' => 'nnap5307-s', + 'nj_loc' => 'nydevnfs_bigstor_volC' + }, + 'filer_volume' => { + 'ny_loc' => '/vol/bigstor_volC/bigstor17 +', + 'nj_loc' => '/vol/bigstor_volC/bigstor17 +' + }, + 'export_name' => '/bb/bigstor17' + },