in reply to Re^6: Perl modules or standard tools for searching hierachical data
in thread Perl modules or standard tools for searching hierachical data

Looking at the code you just posted (thanks for that), I think the problem is that you left out the "chomp;" when reading the records in from the input file. Why did you leave that out? It's pretty important, because without that, every "parent" srting will include the "\r\n" ("CRLF") line-termination characters, and will therefore never match a "child" string (because the child never includes "\r\n" at the end). Please try adding "chomp;" as the first line of the while loop when reading in the data, and see if that helps.

Apart from that, when I put your five lines of sample data (thanks for that) into my original code, it came out in this order:

A1-4100|A1 A1-4100-YZX-002|A1-4100 A1-4100-YZX-002-01|A1-4100-YZX-002 A1-4200|A1 A1-4200-ABC-001|A1-4200
which is just like the one you said would be okay. It might also come out as:
A1-4200|A1 A1-4200-ABC-001|A1-4200 A1-4100|A1 A1-4100-YZX-002|A1-4100 A1-4100-YZX-002-01|A1-4100-YZX-002
and that should also be acceptable. If you got something other than those two possible outputs, it's probably because you forgot to "chomp;" the input, or maybe there are spurious other whitespace characters that you weren't aware of.

As for the initial "TOP" record, are you sure you have that worked out fully? Would it be the case that every top-level parent (identified as such in my script) needs its own "top-level-string|TOP" record? If so, it would be easy to modify the code to make sure this is done for each of the top-level parents -- just add a print statement  print "$parent|TOP\n"; as the first thing in the main "for" loop.

I went ahead and did that on my own copy of the script, and ran it on the huge input sample that you posted above; adding the "TOP" lines like that actually makes it easier to inspect the output for correctness, as follows:

Use the unix "grep -n" command (there are perl versions posted at the Monastery and elsewhere -- e.g. here's mine: grepp -- Perl version of grep) to get the line numbers containing "TOP". Then, for any of those "top-level children", check the line numbers containing "\|top-level-string$". All the latter line numbers should be higher/later in the file than the corresponding TOP line.

Replies are listed 'Best First'.
Re^8: Perl modules or standard tools for searching hierachical data
by SlackBladder (Novice) on Mar 15, 2007 at 10:31 UTC
    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
      You're acting like a doofus. You're missing a simple point. You have records that are delimited by "|" and terminated by "\r\n" (or maybe just "\n"), so:

      As your read your input data record by record, you must use "chomp" to remove the line termination and then you must split on "|". I've already mentioned both of these details in two previous replies. I'm done now. If you still can't get it, I'm sorry for you.

      while (<DATA>) { chomp; # YOU NEED TO DO THIS my ($c, $p) = split( /\|/ ); # THEN DO THIS ...
        Hello, I did say my PERL programming skills were limited ! I have it sorted now, many thanks for your help.
        One thing that was also causing an issue (which I had not been informed of) was the fact that the hierarchy is actually broken anyway :-( No one even mentioned that this would be the case.
        SlackyB (aka Doofus)