To do recursion you really need to do your work in a function. That way, the function can call itself - recurse.
Right now the only function you have is a small helper, uniq and you're not doing the bulk of your work in a function.
Anyway, here's how I'd do it...
use strict;
use warnings;
use Data::Dumper;
# Read the data from the file...
#
my @lines = map { chomp; [split] } <DATA>;
# Arrays are indexed from 0, but we want to count lines
# from 1, so let's unshift a dummy 0 entry onto the array.
#
unshift @lines, undef;
# This is a more usual way of implementing "uniq". It
# preserves the original input order.
#
sub uniq
{
my %seen;
grep { not $seen{$_}++ } @_;
}
# This is the function that does most of the work. It
# calculates what graph theorists would call a "closure"
# over the graph of line references.
#
sub line_closure
{
# We're given an input list, which is a set of
# lines already in the closure.
#
my @input = @_;
# Extend the list by looping through @input to
# generate the output.
#
my @output = uniq map {
# if the line referred to exists
$lines[$_]
# then add the numbers from that
# line
? ($_, @{$lines[$_]})
# otherwise, just keep the existing
# number
: ($_)
} @input;
# If the two lists match (and because we're only
# ever adding to the list, checking the list
# lengths is sufficient), then we've found the
# closure, so return it.
#
if (@output == @input)
{
return @output;
}
# Otherwise, calculate the closure on the expanded
# list.
#
return line_closure(@output);
}
# Get the closure starting at line 1.
#
my @results = line_closure(1);
print Dumper \@results;
__DATA__
1 2 4
2 3 4
3 7
4 6
5 10 11 12 13
6
7 1
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|