in reply to recursive difficulty
use strict; use warnings; my ($curr_string, $in, $select, %immed_child, $all_descends); load_hashes(); process_input(); sub process_input{ while(1){ print qq(enter concept name or "Q" to quit\n); chomp($in = <>); exit() if $in =~ /^\s*q\s*$/i; $in =~s/^\s*(\w+)\s*$/$1/; # still need to check if a valid concept print qq(enter:\n"A" for all descendents,\n"I" for immediate c +hildren\n"P" for parents\n); chomp($select = <>); $select =~ s/^\s*([aip])\s*$/$1/i; print $select, "\n"; show_results(); } } # key: parent # val: list of immediate children sub load_hashes{ my ($child, $par); while(<DATA>){ chomp; ($child, $par) = split/\t/; push @{$immed_child{$par}}, $child; } } #global hash needed to exchange vals w other subs sub get_descends{ my $root = shift; $all_descends->{$root}++ if $root; foreach my $immed (@{$immed_child{$root}}) { get_descends($immed); $all_descends->{$immed}++; } return keys %{$all_descends}; } sub make_display_string{ my $root = shift; my $level = shift ; $curr_string = shift || $root; # $curr_string = "\n". $curr_string; $level++; foreach my $immed (@{$immed_child{$root}}) { $curr_string .="\n". "\t" x $level . $immed; make_display_string($immed, $level, $curr_string); } #$curr_string = "\n" . $curr_string; } sub show_results{ $" = "\n\t"; if ($select =~ m/a/i){ print qq(\ndescendents of \n"$in":\n); $all_descends = {}; my @descends = get_descends($in); print "\t@descends\n"; } elsif ($select =~m/i/i){ if (@{$immed_child{$in}}){ print qq(\nimmediate children of \n"$in":\n); print "\t@{$immed_child{$in}}\n"; } else{ print "no immediate children of $in"; } } elsif ($select =~ m/p/i){ print "working on this\n"; } } __DATA__ vehicle taxonomy exercise device taxonomy computer topic taxonomy automobile vehicle ford automobile chevy automobile bike vehicles bike exercise device jumprope exercise device treadmill exercise device perl computer topic database computer topic
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: recursive difficulty
by jjohhn (Scribe) on Mar 10, 2003 at 00:18 UTC | |
by jjohhn (Scribe) on Mar 10, 2003 at 00:58 UTC |