in reply to Re^3: Writing an array inside an array
in thread Writing an array inside an array

Yeah, it still gives me the error
"Global symbol "$graph" requires explicit package name at line 39".
The code which I have is
use strict; use Graph::Matching; use Graph::Matching qw(max_weight_matching); my @sub = []; my @list = split(',', $ARGV[0]); #print("@list"); my $n = 0; my @graph = (); my $i = 0; my $j = 0; for ($i=0; $n<my $graph; $i++) { for ($j=0; $j<=2; $j++) { $sub[$j] = $list[$n]; $n++; } $graph[$i] = \@sub; @sub = (); }

Thank very much again!

Replies are listed 'Best First'.
Re^5: Writing an array inside an array
by moritz (Cardinal) on Nov 18, 2010 at 13:33 UTC
    The file you've shown us doesn't even have 39 lines.

    Either you've shown us the wrong script, or Graph::Matching does something really scary.

      Yup, sorry I showed only a part of it
      Here is the complete script
      #!/usr/bin/perl BEGIN { push @INC,"C:/strawberry/perl/site/lib"; push @INC,"C:/strawberry/perl/vendor/lib"; push @INC,"C:/strawberry/perl/lib"; } use strict; use Graph::Matching; use Graph::Matching qw(max_weight_matching); my @sub = []; my @list = split(',', $ARGV[0]); #print("@list"); my $n = 0; my @graph = (); my $i = 0; my $j = 0; for ($i=0; $n<my $graph; $i++) { for ($j=0; $j<=2; $j++) { $sub[$j] = $list[$n]; $n++; } $graph[$i] = \@sub; @sub = (); } print "$graph";

      Thank you very much again!
        print "$graph";

        There's no $graph variable in that scope. Did you mean @graph instead?

        You define a useless my $graph variable in the loop header, but that only exists until the end of the block.

Re^5: Writing an array inside an array
by raybies (Chaplain) on Nov 18, 2010 at 13:50 UTC
    it's a minor nitpick, but why use a variable named sub in perl, knowing it's a keyword? if you forget a single sigil you could easily end up with code that runs but does something very unexpected. Also you continue to put "my" in front of graph in two places. In your first for loop, you probably wanted to just put $#graph and leave the second my off. Though because you just initialized your array, you're not going to ever enter that loop until you put something in the @graph array.