in reply to Creating tree with unique categories out of parent-child pointer list

my %seen; while (<>) { chomp; my ($p,$c) = split /:/; ++$seen{$p} if !$seen{$p}; $p .= " $seen{$p}" if $seen{$p} > 1; ++$seen{$c}; $c .= " $seen{$c}" if $seen{$c} > 1; print("$p:$c\n"); }
  • Comment on Re: Creating tree with unique categories out of parent-child pointer list
  • Download Code

Replies are listed 'Best First'.
Re^2: Creating tree with unique categories out of parent-child pointer list
by bryank (Acolyte) on Jun 17, 2009 at 15:16 UTC
    ikegami, that is brilliant. Simple, elegant, awesome. My code tends to be... none of those.

    Anyway, I wanted to make sure I understood the logic. You are basically only checking if a parent is "seen," since a child by definition should be unique? (per the parameters of my request that is?)

      Every child is replaced with a unique string.

      Every parent is replaced with the replacement string used the last time it was seen as a child.

      It assumes the output is depth-first dump of the tree, and that the same key doesn't occur at different levels in a node path.