in reply to Re: recursive difficulty
in thread recursive difficulty

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?

Replies are listed 'Best First'.
Re: Re: 2: recursive difficulty
by pg (Canon) on Mar 10, 2003 at 00:05 UTC
    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.

        The first argument to split is a regex, not a string. /|/ says to match either nothing or nothing. Split, as documented, will split every character when giving a regex that matches nothing.


        Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        theorbtwo answered your problem with split /|/, I suggest that you use the magic split ' ', $line or if working on $_, simply split. This splits on one or more whitespace and ignores empty fields (because of spurious whitespace) at the start and end of your line which should work best in your case.

        -- Hofmator