|
|
| "be consistent" | |
| PerlMonks |
Re: Traverse an unknown multi-dimensional hashby Tomte (Priest) |
| on Jul 12, 2007 at 10:30 UTC ( #626188=note: print w/ replies, xml ) | Need Help?? |
|
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.If printing is all you want, use Data::Dumper or one of its kind as suggested - if you want to code the stuff yourself (anybody smelling homework here?), or have really completly different needs than printing, then have a look at ref, which you can use to determin the datatype of the inspected value, and thus to decide what action to take (recurse into another hash, recurse into array, print a scalar, ...). A recursive implementation will be easiest to implement - however if the structures you expect are deeply nested, I'd use a stack to implement an iterative solution.
regards,
An intellectual is someone whose mind watches itself.
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||