in reply to Re^4: Tree depth
in thread Tree depth

It's confusing... Do you have any ideas?

Yes. Don't remove the use strict; use warnings; from the start of the script. It's there for a reason: it tells you what you are doing wrong. (Namely you're not clearing %graph, but rather $graph, which is a totally different variable, which you've never declared. use strict; catches that errors).

Replies are listed 'Best First'.
Re^6: Tree depth
by katarinahm (Initiate) on Mar 13, 2012 at 15:44 UTC
    This is how my code looks like when I'm using strict and warnings:
    #!/bin/perl; use strict; use warnings; use List::Util qw/max/; my %graph; while (<>) { chomp; (@row) = split(/ /,$_); foreach $item (@row) { ($node,$value) = split(/->/,$item,2); $graph{$node} = $value; } sub depth { return max map { $_ and 1 + depth($graph{$_}) } @_; shift @_ } print depth(keys %graph); print "\n"; @row = (); %graph = "" }

    Here are the error messages that I get:

    Global symbol "@row" requires explicit package name at count_PT.prl li +ne 11. Global symbol "$item" requires explicit package name at count_PT.prl l +ine 12.

    still confusing...

        Now it works!! Thank you for your patience!