in reply to Recursing through hash of arrays

These two lines are at odds:
foreach (@{$found{$sub}}){ print "$tab$found{$sub}[$_]\n";
$_ in the foreach contains the elements, like "a" and "c", but you're using it like an index, like 0, 1, 2. Either walk the index, or walk the list, but not both.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Recursing through hash of arrays
by Popcorn Dave (Abbot) on Jun 24, 2003 at 18:13 UTC
    Thank you for that! At least I was on the right track with it all. And I think I'm beginning to understand a bit more about references. Just haven't got to that chapter of your book yet. :)

    One other thing though. I've changed the subroutine code as you suggested to:

    sub print_tree{ my ($sub, $tab) = @_; foreach (@{$found{$sub}}){ print "$tab$_\n"; $tab .= "\t"; print_tree($_, $tab); } return; }

    but the spacing is still one tab off after the first run through.

    I get:

    a c f e b etc...

    instead of:

    a c f e b etc...

    Any idea on why the $tab variable isn't resetting itself when the recursion starts coming back up it's chain? The e should be in the same column position as c.

    Thanks!

    There is no emoticon for what I'm feeling now.

      Any idea on why the $tab variable isn't resetting itself when the recursion starts coming back up it's chain?
      Yeah, you're messing with $tab in the caller, not the callee. Stop that! Don't append to the value of $tab - simply pass a different value:
      print_tree($_, "\t$tab");

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.