in reply to Traversing a hash of hashes of hashes
This will display hashs in arrays too.
Scalars are not printed, hashs with only scalar values print as {}. arrays with only scalar values print as [].use strict; use warnings; my $hash={ 'flags' => { 'example' => 0, 'font_attributes' => { 'size' => -1, 'colors' => '' }, 'Redacted' => 0, 'Redacted' => [], 'font_changes' => { 'size' => -1, 'colors' => -1 }, 'Redacted' => 0, 'Redacted2' => -1, 'Redacted3' => -1, 'Redacted4' => 0 }, 'Domains' => {}, 'removed_results' => [1,2,3], 'Words' => {}, 'other_results' => {}, 'recipient_list' => [ 'test@test.com' ], 'Clean' => { 'extra' => {} }, 'result' => 'pass', 'Lists' => { 'Body' => { 'RedactedWordList' => { 'words' => { 'word1' => '5', 'word2' => '1', }, 'words_found' => [], 'weight' => 0 } } } ,'1AoH'=>[{h1=>{a=>1}},{h2=>{a=>2}},{h3=>{a=>3}}] ,'2Aoa'=>[[1,2,3],[1,2,3]] ,'3AoAoH'=>[[{a=>1,2=>3},{a=>1,2=>3},{axx=>{a=>1}}]] ,'4HoHoA'=>{ a11=>[{h111=>{a=>1}},{h211=>{a=>2}},{h311=>{a=>3}},11], a22=>[{h122=>{a=>1}},{h222=>{a=>2}},{h322=>{a=>3}},22], } ,'5HoH'=>{ a411=>{h1111=>{a=>1}, ,h4112=>{h41121=>{a=>2}} ,h4113=>{h41131=>{a=>3}} ,h4114=>1 }, a422=>{h422a=>{h4221=>{a=>1,b=>2} ,a4222=>{h42221=>{a=>2}} ,a4223=>{h42231=>{a=>3}} }, h422b=>1 }, a433=>{a=>1,b=>1,c=>1}, } }; deept($hash,'$hash='); print ';'; sub deept{ my $ref =shift; my $name =shift; my $depth =shift; my $wasfirst=shift; $wasfirst=1 unless (defined($wasfirst)) ; unless ($depth) {$depth=0} else {$depth++} my $depthplus=$depth+length($name); my $first=1; my $lastc=''; my $lines=0; my $sub1=sub { unless ($wasfirst){print ','."\n". (' ' x $depth);} print $name; print $_[0]; $lines++; $lastc=$_[1]; }; my $sub2=sub { my $was=deept($_[0],$_[1],$depthplus,$first); if ($was){ $lines+=$was;$first=0;} }; if (ref($ref) eq "HASH") { $sub1->('{','}'); for my $k (sort keys(%$ref)) {$sub2->($ref->{$k},$k.'=>');} } # hash elsif(ref($ref) eq "ARRAY") { $sub1->('[',']'); for my $m (@$ref) {$sub2->($m,'');} } # array if ($lastc){ if ($lines >2 ){print "\n". (' ' x ($depthplus));} print $lastc; } return $lines; } # deept
$hash={1AoH=>[{h1=>{}}, {h2=>{}}, {h3=>{}} ], 2Aoa=>[[], [] ], 3AoAoH=>[[{}, {}, {axx=>{}} ] ], 4HoHoA=>{a11=>[{h111=>{}}, {h211=>{}}, {h311=>{}} ], a22=>[{h122=>{}}, {h222=>{}}, {h322=>{}} ] }, 5HoH=>{a411=>{h1111=>{}, h4112=>{h41121=>{}}, h4113=>{h41131=>{}} }, a422=>{h422a=>{a4222=>{h42221=>{}}, a4223=>{h42231=>{}}, h4221=>{} } }, a433=>{} }, Clean=>{extra=>{}}, Domains=>{}, Lists=>{Body=>{RedactedWordList=>{words=>{}, words_found=>[] } } }, Words=>{}, flags=>{font_attributes=>{}, font_changes=>{} }, other_results=>{}, recipient_list=>[], removed_results=>[] };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Traversing a hash of hashes of hashes
by huck (Prior) on Mar 29, 2017 at 15:40 UTC |