in reply to recursive difficulty

A couple of things. First and foremost you once you have read everything out of <DATA> you would not be able to call the routine again to reload the data. (a simple example should show what i mean):
use strict; use warnings; get_data(); get_data(); sub get_data { while ( <DATA> ) { print $_; } } ##get_data __DATA__ A B C D
Second, instead of popping things off of the array (in the hash) and thus changing the data in the structure, I would use a construct like the following,so that the data structure stays intact and thus there would be no reason to rebuild it. What I mean is that, instead of this:
while(1){ last unless my $immed = pop @{$immed_child{$root}}; get_descends($immed); $all_descends{$immed}++; }
I would do something like this instead:
foreach my $immed ( @{$immed_child{$root}} ) { get_descends($immed); $all_descends{$immed}++; }
Anyhow, I hope this helps.

-enlil

Replies are listed 'Best First'.
Re: 2: recursive difficulty
by jjohhn (Scribe) on Mar 09, 2003 at 23:57 UTC
    Thank you. I did as you suggested, and it seems like my split has now somehow broke: I get the error "Use of unitialized value in concatenation (.) or string at tree.pl line 36, <DATA> line 1 (for all the <DATA> lines).

    I copied and pasted the code from perlmonks onto a different computer; that may be related.

    I thought perhaps my vim expandtabs option on this machine was involved, but I retyped the DATA using ^V. This code

    # 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; print "ch: $child, par: $par\n"; } }
    prints the entire table row as the value of $child, and no value for $par. For example:

    ch: jumprope exercise_device, par:

    this explains the unitialized value in the string. Is there an obvious mistake in my split that has somehow occurred in the copying of the code from perlmonks onto a new machine?

      That's one of the problem I had at the beginning. By copy and paste, all those \t's are gone. I just manually added those \t's back to your __DATA__. Those \t's are what you used in your split.
        That seems not to be working for me, despite using ^V. I think it is my vim expandtab setting. I tried pipes as delimiters, but split /|/ seemed to split between characters for some reason:

        ch: v, par: e
        in my little debug printout.

        I am a litttle baffled.