in reply to accessing element of array of arrays

Two comments, two opinions. Lets test whether FunkyMonk is right:

perl -e ' $A[5][2]= 33; $B[1][1]=4; $B[5][2]= $A[1][1]; $B[1][1]=9; p +rint "A=$A[5][2], B=$B[1][1]\n";' A=33, B=9
You see, your code should be working, like FunkyMonk predicted. You have a bug somewhere else

To understand what [][] means, it is just a short form of []->[]. So you really have only pointers to arrays in each $AoA1[...]. Exactly as in C.

Replies are listed 'Best First'.
Re^2: accessing element of array of arrays
by JadeNB (Chaplain) on Jul 07, 2008 at 23:04 UTC
    Your code doesn't seem to test what the author is discussing -- namely, after your assignment $B[5][2] = $A[1][1], does changing $A[1][1] change $B[5][2]? (Indeed, the only 'entangled' elements of your code are $B[5][2] and $A[1][1], and they aren't logged at the end.) With your choice of indexing, I think that what you want is:
    perl -e '$A[1][1] = 33; $B[5][2] = $A[1][1]; $A[1][1] = 9; print "A = +$A[1][1], B = $B[5][2]\n";'
    This prints out
    A = 9, B = 33
    which is exactly what (I think) you meant, and which certainly seems to be the behaviour desired by the original poster.

    UPDATED (twice) -- I flipped the values of A and B (though I could have sworn I'd cut-and-pasted from the shell), and fixed a mark-up error.