in reply to print arrays a(i), b(i,j) c(i,j)

To extend what GrandFather is saying above, Perl does not use the same type of syntax has C/C++ to refer to a multidimensional array. In C one would say spec_cps[i][j], while in Perl one would say $spec_cps[$i,$j]

Look up the $; special variable to see what Perl is doing to mimic a true multidimensional array.

-imran

Replies are listed 'Best First'.
Re^2: print arrays a(i), b(i,j) c(i,j)
by GrandFather (Saint) on Feb 13, 2006 at 09:05 UTC

    Hmm, not quite. In fact, not at all. $; controls the symbol used as a subscript seperator for multi-dimensional hashes, not arrays. Perl arrays are more like lists - they are dynamiclly sized and can not by managed in the same way that fixed size C arrays can be.

    To achieve multi-dimensional arrays in Perl you need to remember that a scalar can hold a reference to another object, for our purposes it can hold a reference to an array. So we implement multi-dimensional arrays by having an array of references to arrays.

    A little syntactic sugar makes accessing such multi-dimensional arrays identical with the equivelent C syntax. $AoA[0][0] accesses the first element in the first row of the array @AoA. @AoA is really an array of references to arrays. The syntactic sugar saves writing the access as $AoA[0]->[0]; to dereference the first element of @AoA then index into the array referenced by that element. Take a look at the two @xxxx_cps arrays in the code sample in Re: print arrays a(i), b(i,j) c(i,j) to see how they are initialised and how elements in them are accessed


    DWIM is Perl's answer to Gödel