in reply to Writing an array inside an array

Please read Markup in the Monastery, and put <code>...</code> tags around your code.

You probably don't want to assing [] to @sub - that way the array doesn't become empty, but holds a single value, which is a reference to an empty array. Use @sub = () instead.

Also you'll probably want $graph[$i] = \@sub (no my before it. You're assigning to a part of an existing data structure, declaring a new variable doesn't make sense in that context).

Also please read perllol, which talks about arrays of array references.

Replies are listed 'Best First'.
Re^2: Writing an array inside an array
by talha099 (Initiate) on Nov 18, 2010 at 13:03 UTC
    Thank you very much!
    Now, it gives me the error saying
    Global symbol "$graph" requires explicit package name
    in the line
     $graph[$i] = \@sub;
    Can you please help me again?
    Thanks very much!

      try using

      my @graph = ();
      when you initialize the variable at the top of your code. Think of it as a declaration. You should NOT use 'my' again on the variable however.

      HtH

      Misha/Michael - Russian student, grognard, bemused observer of humanity and self professed programmer with delusions of relevance
        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!