Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi everyone,

Say you had an unknown hash structure and you didnt know how many dimensions to it there were. By dimension I mean child hashes, so it could be HoH, HoHoH, HoHoHoH, and so on, you just dont know.

How would you traverse this hash to print out all key => value pairs. The only way I know how is a series of for loops, however that would mean you have to know in advance how deep it goes.

for my $k1 ( sort keys %{ $self->{"_dataSymbols"} } ) { print "$k1\n"; for my $k2 ( sort keys %{ $self->{"_dataSymbols"}->{ $k1 }} ) { print "\t$k2\n"; for my $k3 ( sort keys %{ $self->{"_dataSymbols"}->{ $k1 }->{ + $k2 } } ) { print "\t\t$k3\n"; for my $k4 ( sort keys %{ $self->{"_dataSymbols"}->{ $k1 } +->{ $k2 }->{ $k3 } } ) { print "\t\t\t$k4\n"; for my $k5 ( sort keys %{ $self->{"_dataSymbols"}->{ $k +1 }->{ $k2 }->{ $k3 }->{ $k4 } } ) { print "\t\t\t\t$k5\n"; for my $k6 ( sort keys %{ $self->{"_dataSymbols"}->{ + $k1 }->{ $k2 }->{ $k3 }->{ $k4 }->{ $k5 } } ) { print "\t\t\t\t\t$k6 => %{ $self->{"_dataSymbols" +}->{ $k1 }->{ $k2 }->{ $k3 }->{ $k4 }->{ $k5 } }\n"; } } } } } }
Any help is greatly appreciated, thanks in advance, Mich.

In reply to Traverse an unknown multi-dimensional hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-01-23 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?