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

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

Replies are listed 'Best First'.
Re^9: Perl modules or standard tools for searching hierachical data
by graff (Chancellor) on Mar 15, 2007 at 11:11 UTC
    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)