in reply to Printing a tree-view of a hash of hashes

Post code that we can run to reproduce your failing case.

Replies are listed 'Best First'.
Re^2: Printing a tree-view of a hash of hashes
by pdxperl (Sexton) on Jun 01, 2010 at 02:19 UTC
    OK, this is so odd I know it's something I'm going to be something dumb that I'll be embarrassed about. When I created the testcase, it works fine. When I run the complete program, there's a problem. But here's why that's odd -- instead of posting a lot of code, Excel spreadsheets, etc, I just put a Data::Dumper command for %categories right before I did the print in the original program. Then I created the testcase, copied the output from Data::Dumper and just set %categories to the content of Data::Dumper, then did the print using the same code. Works fine! Now if I run the original program, I see the output from Data::Dumper isn't changed -- but the final printed output is screwed up. I'm confused since the content of %categories looks EXACTLY the same! (I do agree, would be better/more flexible to use recursion! However, the testcase works) Here's the testcase and it's output:

    So.. that's correct. No issues with how I'm reading the hash. However in the main program, the output looks like this. Look at accessories->bookends -- a whole new level of hierarchy at the third level with lots of bogus info!

    <etc... rest of the errors snipped>

      I get Can't use string ("1") as a HASH ref while "strict refs" in use. You seem to be hiding errors by not using use strict; use warnings;. no autovivification; would also be useful to you.

      This kind of mistake elsewhere in your code would definitely explain your symptoms. You're placing all your data in one hash (%1), and you are linking to that hash throughout the tree.

      I think your data structure is equivalent to the one below. It would also explain the infinite looping you are experiencing with the recursive solutions.

      %{"1"} = ( cat2 => \%{"1"}, cat21 => \%{"1"}, cat22 => \%{"1"}, cat3 => \%{"1"}, ); my %category = ( cat1 => \%{"1"}, );