Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: trying to print hashes with mixed results

by NetWallah (Canon)
on Apr 09, 2021 at 03:56 UTC ( [id://11131044]=note: print w/replies, xml ) Need Help??


in reply to trying to print hashes with mixed results

uuse strict; use warnings; sub PrintHash{ my ($href, $indent_text, $level) = @_; $level ||= 0; while (my ($k, $v) = each(%$href)) { print +("\t" x $level), "${indent_text}$k = "; if (not ref $v){ #SCALAR print "$v\n"; }elsif (ref $v eq "HASH"){ print "\{\n"; PrintHash($v,"",$level+1); print +("\t" x $level),"}\n"; }elsif (ref $v eq "ARRAY"){ local $,=", "; print "\@(@$v)\n"; # Could do a better job by writing sub PrintArrayRef{} }else{ die "ERROR: Invalid type:",ref($v),"\n"; } } } PrintHash({Uno=>1, Dos=>2, Tres=>3, Cuatro=>[1,2,3,4], Cinco=>{otro=>9 +9,rojo=>'Red'}}, "Error codes:", 0);
Output:
> perl .\printhash.pl Error codes:Cuatro = @(1 2 3 4) Error codes:Uno = 1 Error codes:Dos = 2 Error codes:Tres = 3 Error codes:Cinco = { otro = 99 rojo = Red }
If you have specific questions abut a specific line of code, go ahead and post it here.

                "Avoid strange women and temporary variables."

Replies are listed 'Best First'.
Re^2: trying to print hashes with mixed results
by merrittr (Novice) on Apr 11, 2021 at 02:57 UTC
    Holy crap ! that works great! thanks NetWallah

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131044]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found