Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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'

In reply to Re: I need help with some recursion by tobyink
in thread I need help with some recursion by ragnarokPP

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2018-05-28 06:20 GMT
    Find Nodes?
      Voting Booth?