#!/usr/bin/perl use strict; use warnings; my %node; my $folder = "P:\\Prod-Operations\\Maint-Eng\\Maintenance Projects\\INF\\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 () { 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}{child_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 parents: # open (INT01,">$folder\\$interset01") or die "Can not open file $folder\\$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 ); } } }