bryank has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am working with a friend to use gui automation software to build a tree structure. The software looks for the name of a parent node, and adds a child to it. A simple parent child spreadsheet works for the most part. The problem is that if a node farther down the list shares the same name as a previous parent (or child for that matter), the automation software doesn't know which node to look for in order to add the child.

I'm looking for a process where I can:

1. look at each node (parent or child), and append a unique identifier if that node name exists already elsewhere.

2. The exception being that a parent node should have all its children assigned to it!

The input file is ordered in the way that the automation script would build the tree, so it is apparent which children go with with parents.

For example (and this is just to show the behavior)

Root:For Her For Her:Clothes -- Clothes would be marked as a child For Her:Pants -- Pants would be marked as a child For Her:Shirts -- Shirts would be marked as a child . . . Scarves:For Her -- 'For Her' is new, and should be unique. Root:Clothes -- 'Clothes' here is new, and should be unique

Any tips would be much appreciated!!

  • Comment on Creating tree with unique categories out of parent-child pointer list
  • Download Code

Replies are listed 'Best First'.
Re: Creating tree with unique categories out of parent-child pointer list
by ikegami (Patriarch) on Jun 17, 2009 at 01:01 UTC
    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"); }
      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.