P is for Practical | |
PerlMonks |
Re^2: Arbitrarily Nested HoHby Limbic~Region (Chancellor) |
on Oct 15, 2010 at 21:26 UTC ( [id://865572]=note: print w/replies, xml ) | Need Help?? |
apomatix,
I wonder how else you thought of doing it. As I indicated, I considered an iterator. Here is a very unpolished working version:
As you can see, it is far more complex than the recursive solution. Also, the iterator could result in bizarre behavior if the hash isn't walked all at once. This is because each itself is an iterator that is reset any time there is a call to keys or values. In the recursive version, the hash is traversed all at once so there isn't an opportunity to reset the iterator mid-traversal. Here is the stack based iterative version I was thinking of. I think it is the nicest because it is how my brain works:
However, I don't think your solution would be easy for someone "just learning Perl". Why? That's an honest question. Provided you understand references (which he does), it should be straight forward. That is unless you are like me and have a hard time thinking recursively but the whole point of writing it recursively was that most programmers I know think that way naturally. What about it do you think a seasoned C++ programmer would find hard to understand? Cheers - L~R
In Section
Seekers of Perl Wisdom
|
|