Hello
I am now beginning to wonder at my own sanity as when I run your original code, replacing your data with the following
A1-4100-YZX-002|A1-4100 A1-4100|A1 A1-4200-ABC-001|A1-4200 A1-4200|A1 A1-4100-YZX-002-01|A1-4100-YZX-002
I get the following output
A1-4100|A1 A1-4200-ABC-001|A1-4200 A1-4100-YZX-002-01|A1-4100-YZX-002 A1-4100-YZX-002|A1-4100 A1-4200|A1
Which is very different to what your seeing. The full code I am running is..........
#!/usr/bin/perl use strict; use warnings; my %node; while (<DATA>) { my ( $c, $p ) = split; if ( $c eq $p ) { # these are easy, so finish them first print; next; } if ( exists( $node{$c}{child_of} )) { warn "$.: bad record: $c is child of both $p and $node{$c}{chi +ld_of}\n"; next; } $node{$c}{child_of} = $p; $node{$p}{parent_of}{$c} = undef; } # begin the sorted output by looping over values that do not have pare +nts: for my $parent ( grep { !exists( $node{$_}{child_of} ) } keys %node ) +{ my $children = $node{$parent}{parent_of}; # ref to hash of child +values trace_down( $children, \%node ); } sub trace_down { my ( $kids, $tree ) = @_; for my $kid ( keys %$kids ) { print "$kid $$tree{$kid}{child_of}\n"; if ( exists( $$tree{$kid}{parent_of} )) { trace_down( $$tree{$kid}{parent_of}, $tree ); } } } __DATA__ A1-4100-YZX-002|A1-4100 A1-4100|A1 A1-4200-ABC-001|A1-4200 A1-4200|A1 A1-4100-YZX-002-01|A1-4100-YZX-002
On the question of multiple "TOPS".... There will only ever be one "TOP" which is A1. The reason for this is that there are around 12 (I can't remember the exact number) "children" of A1 which are A1-4100, A1-4200, A1-4300 etc etc etc. Then you have (for example) "A1-4200-ABC-001". I already tried using "grep -w" (match only whole words) and using "sort" (I worked for a number of years on UNIX and still work with Linux) which I thought may fix things, however, the parent-to-child relationship does not follow strict numbering conventions, i.e. the sequence "A1-4200-ABC-001" may be a child of "A1-405-ABC-001-FF".
The top two tiers of the hierarchy follow a numbering convention (A1, A1-4600 etc) and can be followed quite easily, however, the rest has been built by a person and not by any "logical" numbering system so using partial string matching will not work, unfortunately.

I realize I am taking up a lot of your time with this and I do appreciate your input and patience with my coding inadequacies.
SlackyB

In reply to Re^8: Perl modules or standard tools for searching hierachical data by SlackBladder
in thread Perl modules or standard tools for searching hierachical data by SlackBladder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.