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

Hi,
You are correct when you say you are assuming that I am extracting data into a file, and then working on it. I am perfectly happy to post the connection routines I am using plus the data and time stuff (for a log file)here but I think it may be a little cluttered if I do that, I will post it in the "Code Catacombs" so that it can be re-used if required.......
Let see if I can get the "readmore" TAG to work....
A1-400-EC-001A|A1-4000<br/> A1-400-EC-001B|A1-4000<br/> A1-400-EC-001C|A1-4000<br/> A1-400-EC-001D|A1-4000<br/> A1-400-EC-001E|A1-4000<br/> A1-400-EC-001F|A1-4000<br/> A1-400-EC-001G|A1-4000<br/> A1-400-EC-001H|A1-4000<br/> A1-400-EC-001J|A1-4000<br/> A1-400-EC-001K|A1-4000<br/> A1-400-EHC-001|A1-400-HC-001<br/> A1-400-EKF-001A|A1-400-KF-001A<br/> A1-400-EKF-001B|A1-400-KF-001B<br/> A1-400-EKF-001C|A1-400-KF-001C<br/> A1-400-EKF-001D|A1-400-KF-001D<br/> A1-400-EKF-001E|A1-400-KF-001E<br/> A1-400-EKF-001F|A1-400-KF-001F<br/> A1-400-EKF-001G|A1-400-KF-001G<br/> A1-400-EKF-001H|A1-400-KF-001H<br/> A1-400-EKF-001J|A1-400-KF-001J<br/> A1-400-EKF-001K|A1-400-KF-001K<br/> A1-400-EKF-001L|A1-400-KF-001L<br/> A1-400-EKF-001M|A1-400-KF-001M<br/> A1-400-EN-003A|A1-4000<br/> A1-400-EN-003B|A1-4000<br/> A1-400-EPA-001A|A1-400-PA-001A<br/> A1-400-EPA-001B|A1-400-PA-001B<br/> A1-400-EPA-001C|A1-400-PA-001C<br/> A1-400-EPA-001D|A1-400-PA-001D<br/> A1-400-EPA-002A|A1-400-PA-002A<br/> A1-400-EPA-002B|A1-400-PA-002B<br/> A1-400-EPB-001|A1-400-PB-001<br/> A1-400-EPB-002A|A1-400-PB-002A<br/> A1-400-EPB-002B|A1-400-PB-002B<br/> A1-400-HA-001|A1-4000<br/> A1-400-HC-001|A1-4000<br/> A1-400-HX-001A|A1-400-HC-001<br/> A1-400-HX-001B|A1-400-HC-001<br/> A1-400-HX-001C|A1-400-HC-001<br/> A1-400-HX-001D|A1-400-HC-001<br/> A1-400-HX-001E|A1-400-HC-001<br/> A1-400-HX-001F|A1-400-HC-001<br/> A1-400-HX-001G|A1-400-HC-001<br/> A1-400-HX-001H|A1-400-HC-001<br/> A1-400-HX-001J|A1-400-HC-001<br/> A1-400-HX-001K|A1-400-HC-001<br/> A1-400-KF-001A|A1-400-HC-001<br/> A1-400-KF-001B|A1-400-HC-001<br/> A1-400-KF-001C|A1-400-HC-001<br/> A1-400-KF-001D|A1-400-HC-001<br/> A1-400-KF-001E|A1-400-HC-001<br/> A1-400-KF-001F|A1-400-HC-001<br/> A1-400-KF-001G|A1-400-HC-001<br/> A1-400-KF-001H|A1-400-HC-001<br/> A1-400-KF-001J|A1-400-HC-001<br/> A1-400-KF-001K|A1-400-HC-001<br/> A1-400-PA-001A|A1-400-HC-001<br/> A1-400-PA-001B|A1-4000<br/> A1-400-PA-001C|A1-4000<br/> A1-400-PA-001D|A1-4000<br/> A1-400-PA-002A|A1-4000<br/> A1-400-PA-002B|A1-4000<br/> A1-400-PB-001|A1-4000<br/> A1-400-PB-002A|A1-4000<br/> A1-400-PB-002B|A1-4000<br/> A1-400-TA-001|A1-4000<br/> A1-400-VB-001|A1-4000<br/> A1-400-ZD-001|A1-400-TA-001<br/> A1-4000|A1<br/> A1-4000-AG-001-6-A21-WN|A1-4000<br/> A1-4000-CM-001-3-A21-BP|A1-4000<br/> A1-4000-CM-002-3-A21-BP|A1-4000<br/> A1-4000-CM-003-2-A21-BP|A1-4000<br/> A1-4000-CM-004-2-A21-BP|A1-4000<br/> A1-4000-CM-005-2-A21-BP|A1-4000<br/> A1-4000-CM-007-2-A21-BP|A1-4000<br/> A1-4000-CM-009-10-A21-HC|A1-4000<br/> A1-4000-CM-011-2-A21-BP|A1-4000<br/> A1-4000-CM-012-2-A21-BP|A1-4000<br/> A1-4000-CM-013-2-A21-BP|A1-4000<br/> A1-4000-CM-015-2-A05-BP|A1-4000<br/> A1-4000-CM-019-2-A21-BP|A1-4000<br/> A1-4000-CM-020-20-A21-BP|A1-4000<br/> A1-4000-CM-021-16-A21-BP|A1-4000<br/> A1-4000-CM-022-16-A21-BP|A1-4000<br/> A1-4000-CM-023-16-A21-BP|A1-4000<br/> A1-4000-CM-024-16-A21-BP|A1-4000<br/> A1-4000-CM-026-20-A21-HC|A1-4000<br/> A1-4000-CM-029-12-A21-HC|A1-4000<br/> A1-4000-CM-030-20-A21-BP|A1-4000<br/> A1-4000-CM-031-16-A21-BP|A1-4000<br/> A1-4000-CM-032-16-A21-BP|A1-4000<br/> A1-4000-CM-033-16-A21-BP|A1-4000<br/> A1-4000-CM-034-16-A21-BP|A1-4000<br/> A1-4000-CM-036-16-A21-HC|A1-4000<br/> A1-4000-CM-040-20-A21-HC|A1-4000<br/> A1-4000-CM-041-10-A21-HC|A1-4000<br/> A1-4000-CM-042-10-A21-HC|A1-4000<br/> A1-4000-CM-043-10-A21-HC|A1-4000<br/> A1-4000-CM-044-10-A21-HC|A1-4000<br/> A1-4000-CM-045-10-A21-HC|A1-4000<br/> A1-4000-CM-050-20-A21-HC|A1-4000<br/> A1-4000-CM-051-10-A21-HC|A1-4000<br/> A1-4000-CM-052-10-A21-HC|A1-4000<br/> A1-4000-CM-053-10-A21-HC|A1-4000<br/> A1-4000-CM-054-10-A21-HC|A1-4000<br/> A1-4000-CM-055-10-A21-HC|A1-4000<br/> A1-4000-CM-061-4-A21-HC|A1-4000<br/> A1-4000-CM-062-4-A21-HC|A1-4000<br/> A1-4000-CM-064-1-A21-HC|A1-4000<br/> A1-4000-CM-065-1-A21-PP|A1-4000<br/> A1-4000-CM-071-10-A21-BP|A1-4000<br/> A1-4000-CM-072-12-A21-BP|A1-4000<br/> A1-4000-CM-073-16-A21-HC|A1-4000<br/> A1-4000-CM-074-16-A21-HC|A1-4000<br/> A1-4000-CM-075-12-A21-HC|A1-4000<br/> A1-4000-CM-077-10-A21-HC|A1-4000<br/> A1-4000-CM-078-12-A21-BP|A1-4000<br/> A1-4000-DMC-2211-01|A1-4000<br/> A1-4000-FCV-703A|A1-4000-CM-071-10-A21-BP<br/> A1-4000-FCV-703B|A1-4000-CM-036-16-A21-HC<br/> A1-4000-FIC-703A|A1-4000-FCV-703A<br/> A1-4000-FIC-703B|A1-4000-FCV-703B<br/> A1-4000-FT-703|A1-4000-FCV-703A<br/> A1-4000-FY-703A|A1-4000-FCV-703A<br/> A1-4000-FY-703B|A1-4000-FCV-703B<br/> A1-4000-GY-001-3/4-A21-BP|A1-4000<br/> A1-4000-GY-002-1 1/2-A21-BP|A1-4000<br/> A1-4000-GY-003-2-A21-BP|A1-4000<br/> A1-4000-LA-703|A1-4000-LT-705<br/> A1-4000-LA-707|A1-4000-LT-707B<br/> A1-4000-LG-702|A1-400-VB-001<br/> A1-4000-LI-703|A1-4000-LT-703<br/> A1-4000-LI-705|A1-4000-LT-705<br/> A1-4000-LI-707A|A1-4000-LT-707A<br/> A1-4000-LI-707B|A1-4000-LT-707B<br/> A1-4000-LI-707C|A1-4000-LT-707C<br/> A1-4000-LIC-701|A1-4000-LT-701<br/> A1-4000-LT-701|A1-400-VB-001<br/> A1-4000-LT-703|A1-400-TA-001<br/> A1-4000-LT-705|A1-400-TA-001<br/> A1-4000-LT-707A|A1-400-VB-001<br/> A1-4000-LT-707B|A1-400-VB-001<br/> A1-4000-LT-707C|A1-400-VB-001<br/> A1-4000-PG-701A|A1-400-PA-001A<br/> A1-4000-PG-701B|A1-400-PA-001B<br/> A1-4000-PG-701C|A1-400-PA-001C<br/> A1-4000-PG-701D|A1-400-PA-001D<br/> A1-4000-PG-702A|A1-400-PA-002A<br/> <br/>
This is the code (all of it) that I am using the run over the extracted data...
#!/usr/bin/perl<br/> use strict;<br/> use warnings;<br/> <br/> my %node;<br/> my $folder = "P:\\Prod-Operations\\Maint-Eng\\Maintenance Projects\\IN +F\\DB\\IFDB\\TAGHIERARCHY\\PerlScripts";<br/> my $resultfile = "resultset.txt";<br/> my $interset01 = "interset01.txt";<br/> my $interset02 = "interset02.txt";<br/> <br/> open (DATA, "< $folder\\$resultfile") || die "could not open file: $!" +;<br/> while (<DATA>) {<br/> my ( $c, $p ) = split /\|/;<br/> if ( $c eq $p ) { # these are easy, so finish them first<br/> print;<br/> next;<br/> }<br/> <br/> if ( exists( $node{$c}{child_of} )) {<br/> warn "$.: bad record: $c is child of both $p and $node{$c}{chi +ld_of}\n";<br/> next;<br/> }<br/> $node{$c}{child_of} = $p;<br/> $node{$p}{parent_of}{$c} = undef;<br/> }<br/> <br/> # begin the sorted output by looping over values that do not have pare +nts:<br/> open (INT01,">$folder\\$interset01") or die "Can not open file $folder +\\$interset01 for writing, quitting\n";<br/> <br/> for my $parent ( grep {!exists( $node{$_}{child_of} ) } keys %node ) { +<br/> my $children = $node{$parent}{parent_of}; # ref to hash of child +values<br/> trace_down( $children, \%node );<br/> }<br/> <br/> sub trace_down<br/> {<br/> my ( $kids, $tree ) = @_;<br/> for my $kid ( keys %$kids ) {<br/> print INT01 "$kid|$$tree{$kid}{child_of}";<br/> # print "$kid|$$tree{$kid}{child_of}\n";<br/> if ( exists( $$tree{$kid}{parent_of} )) {<br/> trace_down( $$tree{$kid}{parent_of}, $tree );<br/> }<br/> }<br/> }<br/> <br/> <br/>
Here is a sample from the result set....
<readmore> title "Result set sample"><br/> A1-4708-TT-732A|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PT-702|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PSV-704B|A1-4708-LS-001-30-B40-HC<br/> A1-4708-RG-761|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PT-711C|A1-4708-LS-001-30-B40-HC<br/> A1-4708-TT-732C|A1-4708-LS-001-30-B40-HC<br/> A1-4708-XV-700|A1-4708-LS-001-30-B40-HC<br/> A1-4708-TT-702|A1-4708-LS-001-30-B40-HC<br/> A1-4708-TT-732B|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PSV-704C|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PSV-704D|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PSV-704A|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PT-711B|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PG-703|A1-4708-LS-001-30-B40-HC<br/> A1-4708-PT-711A|A1-4708-LS-001-30-B40-HC<br/> A1-690-EGF-012C|A1-690-GF-012C<br/> A1-4800-RG-808|A1-4800-FO-029-1-A21-HC<br/> A1-4800-RG-809|A1-4800-FO-029-1-A21-HC<br/> A1-4800-PSV-708|A1-4800-FO-029-1-A21-HC<br/> A1-6900-RF-769|A1-6900-HM-042-6-A21-HC<br/> A1-4707-RB-770|A1-4600-IA-200-2-A21-BP<br/> A1-6900-TIC-713|A1-6900-TE-713<br/> A1-4000-VSH-711F|A1-400-KF-001F<br/> A1-400-EKF-001F|A1-400-KF-001F<br/> A1-600-XX-004~FCV353|A1-600-XX-004<br/> A1-600-XX-004~F01|A1-600-XX-004<br/> A1-600-XX-004~F02|A1-600-XX-004<br/> A1-600-XX-004~KV311|A1-600-XX-004<br/> A1-600-XX-004~VAG_S01C|A1-600-XX-004<br/> A1-600-XX-004~F03|A1-600-XX-004<br/> A1-600-XX-004~VAG_S01D|A1-600-XX-004<br/> A1-600-XX-004~KY354|A1-600-XX-004<br/> A1-600-XX-004~VAG_S01B|A1-600-XX-004<br/> A1-600-XX-004~PCV353|A1-600-XX-004<br/> A1-600-XX-004~VAG03|A1-600-XX-004<br/> A1-600-XX-004~E02B|A1-600-XX-004<br/> A1-600-XX-004~FO356|A1-600-XX-004<br/> A1-6000-JC-701|A1-600-XX-004<br/> A1-600-XX-004~E02A|A1-600-XX-004<br/> A1-600-XX-004~E01|A1-600-XX-004<br/> A1-600-XX-004~KY355|A1-600-XX-004<br/> A1-600-XX-004~F04|A1-600-XX-004<br/> A1-6000-RB-750|A1-600-XX-004<br/> A1-600-XX-004~VAG02|A1-600-XX-004<br/> A1-600-XX-004~S01B|A1-600-XX-004<br/> A1-600-XX-004~CVNG354|A1-600-XX-004<br/> A1-600-XX-004~E03|A1-600-XX-004<br/> A1-600-XX-004~F01C|A1-600-XX-004<br/> A1-600-XX-004~R01|A1-600-XX-004<br/> A1-4708-PDT-003|A1-4708-HS-002-20-C40-HC<br/> A1-4708-PCV-766|A1-4708-HS-002-20-C40-HC<br/> A1-4800-RG-770|A1-4800-FO-043-2-A21-HC<br/> A1-4800-RC-751|A1-4800-FO-043-2-A21-HC<br/> A1-4600-USH-703A|A1-460-EKF-003A<br/> A1-4600-USL-703A|A1-460-EKF-003A<br/> A1-4600-UA-703A|A1-460-EKF-003A<br/> A1-4600-UL-703A|A1-460-EKF-003A<br/> A1-4600-UB-703A|A1-460-EKF-003A<br/> A1-470-MT-011~33VG-11|A1-470-MT-011~20VG-1<br/> A1-4707-RA-781|A1-470-HA-071A<br/> A1-4707-PSV-004|A1-470-HA-071A<br/> A1-4707-TG-005|A1-470-HA-071A<br/> A1-4707-RF-751|A1-470-HA-071A<br/> A1-945-EY-003B|A1-945-ET-003B<br/> A1-730-ST-020|A1-7300-FW-339-1-A21-BP<br/> A1-470-MT-031~PDSH158|A1-470-MT-031~PDI158<br/> A1-4200-PZI-703D|A1-4200-PZT-703D<br/> A1-690-EGF-041J|A1-690-GF-041J<br/> A1-6900-LALL-712|A1-6900-LSLL-712A<br/> A1-4702-EZSH-012|A1-4702-EDV-012<br/> A1-4702-EZSL-012|A1-4702-EDV-012<br/> A1-4702-EXY-012|A1-4702-EDV-012<br/> A1-4702-EPT-012|A1-4702-EDV-012<br/> A1-4200-RC-762|A1-4200-CD-484-2-B13-WN<br/> A1-470-MS-071~TAHH-749|A1-470-MS-071~TSHH-749<br/> A1-470-MT-021~AT-ID-2|A1-470-ZA-021<br/> A1-470-MT-021~96RH-1|A1-470-ZA-021<br/> A1-470-MT-021~AT-ID-4|A1-470-ZA-021<br/> A1-470-MT-021~AT-ID-1|A1-470-ZA-021<br/> A1-470-MT-021~AT-ID-3|A1-470-ZA-021<br/> A1-470-MT-021~96RH|A1-470-ZA-021<br/> A1-470-MT-021~AT-ID-6|A1-470-ZA-021<br/> A1-470-MT-021~AT-ID-5|A1-470-ZA-021<br/> A1-4200-RB-776|A1-4200-CD-461-2-B13-WN<br/> A1-4200-RA-771|A1-4200-CD-461-2-B13-WN<br/> A1-470-XY-026|A1-470-XX-021<br/> A1-470-MT-021~HV439|A1-470-XX-021<br/> A1-470-MT-021~HV409|A1-470-XX-021<br/> A1-470-MT-021~HV402|A1-470-XX-021<br/> A1-470-XY-020|A1-470-XX-021<br/> A1-470-MT-021~HV431|A1-470-XX-021<br/> A1-470-XY-021|A1-470-XX-021<br/> A1-470-MT-021|A1-470-XX-021<br/> A1-470-MT-021~HV451B|A1-470-XX-021<br/> A1-470-MT-021~HV440|A1-470-XX-021<br/> A1-470-EG-021|A1-470-XX-021<br/> A1-470-XY-024|A1-470-XX-021<br/> A1-470-MT-021~HV406|A1-470-XX-021<br/> A1-470-MT-021~HV434|A1-470-XX-021<br/> A1-470-MT-021~HV415|A1-470-XX-021<br/> A1-470-XY-023|A1-470-XX-021<br/> A1-470-MT-021~HV405|A1-470-XX-021<br/> A1-470-XY-022|A1-470-XX-021<br/> A1-470-MT-021~HV413|A1-470-XX-021<br/> A1-470-ES-021|A1-470-XX-021<br/> A1-470-MT-021~HV442|A1-470-XX-021<br/> A1-470-MT-021~HV424|A1-470-XX-021<br/> A1-470-MT-021~HV403|A1-470-XX-021<br/> A1-470-LN-021|A1-470-XX-021<br/> A1-470-MT-021~HV408|A1-470-XX-021<br/> A1-470-MT-021~HV423|A1-470-XX-021<br/> A1-470-MT-021~HV437|A1-470-XX-021<br/> A1-470-MT-021~HV451A|A1-470-XX-021<br/> A1-470-MT-021~HV435|A1-470-XX-021<br/> A1-470-MT-021~HV433|A1-470-XX-021<br/> A1-470-MT-021~HV438|A1-470-XX-021<br/> A1-470-MT-021~HV422|A1-470-XX-021<br/> A1-470-ET-021|A1-470-XX-021<br/> A1-470-MT-021~HV432|A1-470-XX-021<br/> A1-470-EE-021|A1-470-XX-021<br/> A1-470-EB-021|A1-470-XX-021<br/> A1-470-MT-021~HV436|A1-470-XX-021<br/> A1-470-MT-021~HV421|A1-470-XX-021<br/> A1-470-MT-021~HV404|A1-470-XX-021<br/> A1-470-EE-022|A1-470-XX-021<br/> A1-470-ZA-021|A1-470-XX-021<br/> A1-470-MT-021~HV441|A1-470-XX-021<br/> A1-470-MT-021~HV407|A1-470-XX-021<br/> A1-480-KF-013B|A1-480-HC-013<br/> A1-480-KF-013A|A1-480-HC-013<br/> A1-480-EHC-013|A1-480-HC-013<br/> A1-4200-PT-709|A1-4200-FG-312-10-B13-HC<br/> A1-4200-TT-709|A1-4200-FG-312-10-B13-HC<br/> A1-4200-FT-709|A1-4200-FG-312-10-B13-HC<br/> A1-4200-RB-767|A1-4200-FG-312-10-B13-HC<br/> A1-4200-RB-766|A1-4200-FG-312-10-B13-HC<br/> A1-4707-RA-753|A1-4707-AG-001-2-B40-PP<br/> A1-4707-RG-768|A1-4707-AG-001-2-B40-PP<br/> A1-470-MT-031~VCK3-2|A1-470-MT-031~PH2<br/> A1-470-MT-031~VR22|A1-470-MT-031~PH2<br/> A1-470-MT-031~88HQ-1|A1-470-MT-031~PH2<br/> A1-6900-RF-768|A1-6900-HM-032-6-A21-HC<br/> A1-4707-PCV-766|A1-4707-HS-002-20-C40-HC<br/> A1-4707-PDT-003|A1-4707-HS-002-20-C40-HC<br/> A1-4800-JF-701|A1-4800<br/> A1-4800-FO-064-2-A21-HC|A1-4800<br/> A1-4800-CM-514-6-A21-HC|A1-4800<br/> A1-4800-FO-038-3-A21-HC|A1-4800<br/> A1-480-ES-009|A1-4800<br/> A1-4800-AG-002-4-A21-WN|A1-4800<br/> <br/> <br/>
I was hoping that the number "A1-4100|A1" would be fairly close to the top of the "sorted" list as "A1-4100" is the parent for a ton of numbers, however, it appears at line 1705 (not show as I think I have probably killed the web site already with the data I have posted !!!)
regards
Nigel

Replies are listed 'Best First'.
Re^5: Perl modules or standard tools for searching hierachical data
by graff (Chancellor) on Mar 13, 2007 at 19:06 UTC
    Please put "code" tags around source code and sample data, like this:

    <code>
    code or data here...
    </code>

    or like this:

    <c>
    code or data here...
    </c>

    When you preview your post, you'll find links to formatting tips for PM.

    Now, as to your sample data: you didn't do what I asked. You posted one set of input, and a completely separate set of output. The two sets seem to have nothing in common apart from the formatting, and your closing comment doesn't make sense:

    I was hoping that the number "A1-4100|A1" would be fairly close to the top of the "sorted" list as "A1-4100" is the parent for a ton of numbers, however, it appears at line 1705 (...)
    Huh? Why would you care about the specific line number where a given record actually shows up in the output? All that matters is that all dependent records come after it.

    If that is not the case for a given run, create a small subset of the data to serve as a test set, run that through the script, and if you can show that the ordering is incorrect, post just that (hopefully small) minimal set of records that demonstrate the error.

    Note that the actual ordering of output from my original code should be: the first top-level parent, then all records dependent on that, then the next top-level parent, then all records dependent on that, and so on.

    (Update: note that the notion of "first top-level parent" is arbitrary; the "keys" function returens hash keys in essentially random order, so if you want to see top-level parents handled in a specific order, you'll need to sort them before they go into the grep function.)

      Apologies for the previous "long" message..
      The reason I care about the order of the line numbers is that this is the order that the data will be loaded into the new RDBMS. A query is going to loop through the file to obtain result sets based on the values in this file. The target RDBMS (SAP) is configured so that if there is an attempt to load a child object before it's parent then the whole import fails. So, as an example, if you have the following records in a source file, where the record on the left is the child and the one on the right is the parent...
      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

      In this order the data will not load. What I am trying to do is re-order the same data as....
      A1|TOP
      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

      This would also load....
      A1|TOP
      A1-4100|A1
      A1-4200|A1
      A1-4100-YZX-002|A1-4100
      A1-4100-YZX-002-01|A1-4100-YZX-002
      A1-4200-ABC-001|A1-4200
      I don't need to worry about the very,very top number "A1" as this is pre-configured in the target database as the highest-on-high, everything else needs to follow below.

      I think that the problem may (and I stress "may") be that within the data, there are "child, father, grandfather" relationships, however, your last statement about the "random" order of the returned hash keys and returning the result set in a specific order by sorting "before" the grep function may be the "key" (sorry for the pun on the word key, couldn't help it)
      regards
      SlackyB
        Hello,
        I forgot to post the code I am using
        #!/usr/bin/perl use strict; use warnings; my %node; my $folder = "P:\\Prod-Operations\\Maint-Eng\\Maintenance Projects\\IN +F\\DB\\IFDB\\TAGHIERARCHY\\PerlScripts"; my $resultfile = "resultset.txt"; my $interset01 = "interset01.txt"; my $interset02 = "interset02.txt"; open (DATA, "< $folder\\$resultfile") || die "could not open file: $!" +; 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: # open (INT01,">$folder\\$interset01") or die "Can not open file $fold +er\\$interset01 for writing, quitting\n"; 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 INT01 "$kid|$$tree{$kid}{child_of}"; print "$kid|$$tree{$kid}{child_of}\n"; if ( exists( $$tree{$kid}{parent_of} )) { trace_down( $$tree{$kid}{parent_of}, $tree ); } } }
        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.