S
slash
Hi,
I need some desperate help with my references usage in the code below.
I'd appreciate the help enormously.
Following is the input in the "data" file:
quick brown fox
brown quick
brown fox jumped
fox brown quick
fox jumped over
fox resting over
jumped fox brown
jumped over lazy
over jumped fox
over lazy dog
lazy over jumped
lazy dog tripped
dog lazy over
dog tripped over
tripped dog lazy
tripped over resting
over tripped dog
over resting fox
resting tripped over
resting fox
Following is the code:
use strict;
my %C; # arrayref of children for a parent
my %P; # parent for a child
open (D, "<data") or die;
while (<D>) {
chomp;
my ($p, $c) = split /\s+/;
next if ((grep /^$p$/, @{$C{$c}}) || $p eq $c);
# add the child to the list of this parent's children
# unless we've already done so
push (@{$C{$p}}, $c) unless (grep /^$c$/, @{$C{$p}});
# record the parent for this child
$P{$c} = $p;
}
close (D);
my @T; # top levels - parents without parents :^)
foreach (keys %C) {
push (@T, $_) unless $P{$_};
}
# build the chart, starting with top levels
my $level = 0;
children($level, \@T);
sub children {
my ($level, $arrayref) = @_;
foreach (sort @$arrayref) {
# show indented
print (" " x $level);
print "$_\n";
# show the children (if any)
children($level+1, $C{$_}) if $C{$_};
}
}
---------------------------------------
And the output I seek is the following:
quick
brown
fox
brown
quick
fox
jumped
fox
brown
quick
resting
over
jumped
over
jumped
fox
brown
over
lazy
over
jumped
fox
tripped
dog
lazy
dog
resting
fox
lazy
over
jumped
dog
tripped
dog
lazy
over
tripped
over
tripped
dog
lazy
over
resting
resting
over
tripped
fox
When I run the above code, I get an infinite loop but I am not sure
where the mistake might be. I have been fiddling with this problem for
long now and any help would be greatly appreciated.
Thanks,
Slash
I need some desperate help with my references usage in the code below.
I'd appreciate the help enormously.
Following is the input in the "data" file:
quick brown fox
brown quick
brown fox jumped
fox brown quick
fox jumped over
fox resting over
jumped fox brown
jumped over lazy
over jumped fox
over lazy dog
lazy over jumped
lazy dog tripped
dog lazy over
dog tripped over
tripped dog lazy
tripped over resting
over tripped dog
over resting fox
resting tripped over
resting fox
Following is the code:
use strict;
my %C; # arrayref of children for a parent
my %P; # parent for a child
open (D, "<data") or die;
while (<D>) {
chomp;
my ($p, $c) = split /\s+/;
next if ((grep /^$p$/, @{$C{$c}}) || $p eq $c);
# add the child to the list of this parent's children
# unless we've already done so
push (@{$C{$p}}, $c) unless (grep /^$c$/, @{$C{$p}});
# record the parent for this child
$P{$c} = $p;
}
close (D);
my @T; # top levels - parents without parents :^)
foreach (keys %C) {
push (@T, $_) unless $P{$_};
}
# build the chart, starting with top levels
my $level = 0;
children($level, \@T);
sub children {
my ($level, $arrayref) = @_;
foreach (sort @$arrayref) {
# show indented
print (" " x $level);
print "$_\n";
# show the children (if any)
children($level+1, $C{$_}) if $C{$_};
}
}
---------------------------------------
And the output I seek is the following:
quick
brown
fox
brown
quick
fox
jumped
fox
brown
quick
resting
over
jumped
over
jumped
fox
brown
over
lazy
over
jumped
fox
tripped
dog
lazy
dog
resting
fox
lazy
over
jumped
dog
tripped
dog
lazy
over
tripped
over
tripped
dog
lazy
over
resting
resting
over
tripped
fox
When I run the above code, I get an infinite loop but I am not sure
where the mistake might be. I have been fiddling with this problem for
long now and any help would be greatly appreciated.
Thanks,
Slash