in reply to 2 D array

The following creates 361 + 361 cells, not 361 x 361:

my @a = ((0) x 361); my @b = ((0) x 361);

For* each of the 361 rows, you need to create 361 cells.

* — Hint!

Replies are listed 'Best First'.
Re^2: 2 D array
by topaz (Initiate) on Jul 28, 2011 at 23:53 UTC
    @row = ((0) x 361); @column = ((0) x 361); my @Matrix = ( @row, @column); ?

      You didn't change anything but the names of the variables?!? That's still the same as

      my @Matrix = (0) x (361*2);

      You want to end up with something equivalent to

      my @Matrix = ( [ (0) x 361 ], [ (0) x 361 ], [ (0) x 361 ], ... 355 more ... [ (0) x 361 ], [ (0) x 361 ], [ (0) x 361 ], );

      Unless you want 361 identical lines, you'll want some kind of loop...

      (I'm treating this as homework. Whether it's personal or assigned doesn't matter to me.)

      Keep in mind that an array is a list of scalar "things".

      Luckily, since array references are scalar "things", your primary array can be made to hold 361 scalar references, each of which is referencing a unique 361-element array.

      For those "secondary" arrays, your job is to somehow create 361 lists (arrays) and still be able to access them as scalar references. But hey, you can get one of those with [(0) x 361]

      You only need to push 360 more onto the primary list where you stored that first one. :-)

      Note: thanks to ikegami for pointing out the mistake with my previous statement that:

      @row = ((0) x 361); is creating a one-element list whose element is a scalar string of 361 zeros.('0000000000000000 ... 00000')

        So, @row = ((0) x 361); is creating a one-element list whose element is a scalar string of 361 zeros.('0000000000000000 ... 00000')

        No. 0 x 361 would do that. (0) x 361 creates a list of 361 zeroes.

        >perl -E"say for (0) x 5;" 0 0 0 0 0 >perl -E"say for 0 x 5;" 00000

        Yeah, that's a pretty quirky syntax.