fingerfunk
Joined: Oct 31, 2012 Posts: 2 Location: Los Angeles, CA
|
Posted: Wed Oct 31, 2012 6:54 pm Post subject:
Writing to multidimensional arrays -- 2d array into 3d array Subject description: Possible to chuck a 2D array into a specific location on third dimension of 3D array? |
|
|
Hi everybody. I'm new to this forum, this is my first post. I've found a few helpful posts about ChucK here -- thanks. I'm relatively new to ChucK also (have been learning it for a couple months), and programming in general.
My question concerns multidimensional arrays (3D arrays, specifically). I'm trying to better understand how write to them.
My basic question is: is it possible to chuck a 2D array to a specific location along the third dimension (i.e. not either of the two dimension of the 2D array being chucked) of a 3D array?
I was having trouble understanding how to write to / access different dimensions of a 3D array, so I did some tests to figure it out. Here's my test:
Code: |
[ [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
[17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
[[33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48],
[49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ] @=> int A[][][];
<<<"A[0][0][0]: ",A[0][0][0]>>>;
<<<"A[0][1][0]: ",A[0][1][0]>>>;
<<<"A[0][1][1]: ",A[0][1][1]>>>;
<<<"A[1][0][0]: ",A[1][0][0]>>>;
<<<"A[1][1][0]: ",A[1][1][0]>>>;
<<<"A[1][1][1]: ",A[1][1][1]>>>;
150 @=> A[1][2][0];
<<<"A[1][2][0]: ",A[1][2][0]>>>;
|
The program prints the following:
Code: |
[chuck](VM): sporking incoming shred: 1 (3D matrix tests.ck)...
A[0][0][0]: 1
A[0][1][0]: 17
A[0][1][1]: 18
A[1][0][0]: 33
A[1][1][0]: 49
A[1][1][1]: 50
A[1][2][0]: 150
|
So far so good. I understand how to instantiate the array in 3 dimensions. I understand how chuck refers to the different positions in the array. I understand how to read specific location in the array. And I understand how to write to the array, two different ways. The way I think of the first method used here is that I'm sending a couple of different 2D arrays to a 3D array. The second method sends a specific value to a specific location in the array.
But what if, later on, I want to send another 2D array to the 3D array? Is there a way to reference a specific position along the 3rd dimension (or 1st dimension, however you think about it), and send a 2D array to it? I can't seem to figure anything out.
I tried the following:
Code: |
[[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],
[81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] @=> A[2][][];
|
But I get a syntax error on the last line.
I also tried this:
Code: |
[[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],
[81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] @=> A[2][0][0];
|
But that produces the following error:
Code: |
[3D matrix tests.ck]:line(63): cannot assign '@=>' on types 'int[][]' @=> 'int'...
[3D matrix tests.ck]:line(63): ...(reason: --- incompatible types for assignment |
Any ideas? Is it possible to do what I'm trying to do? Am I making sense?
Thanks in advance! |
|
fingerfunk
Joined: Oct 31, 2012 Posts: 2 Location: Los Angeles, CA
|
Posted: Thu Nov 01, 2012 1:29 am Post subject:
|
|
|
Thanks! Your solution didn't seem to work for me though. I tried it and got an "array out of bounds" message.
Here's the entire test program:
Code: |
[ [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
[17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
[[33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48],
[49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ] @=> int A[][][];
<<<"A[0][0][0]: ",A[0][0][0]>>>;
<<<"A[0][1][0]: ",A[0][1][0]>>>;
<<<"A[0][1][1]: ",A[0][1][1]>>>;
<<<"A[1][0][0]: ",A[1][0][0]>>>;
<<<"A[1][1][0]: ",A[1][1][0]>>>;
<<<"A[1][1][1]: ",A[1][1][1]>>>;
150 @=> A[1][2][0];
<<<"A[1][2][0]: ",A[1][2][0]>>>;
[[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],
[81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] @=> A[2];
|
Here's what it printed:
Code: |
[chuck](VM): sporking incoming shred: 1 (3D matrix tests.ck)...
A[0][0][0]: 1
A[0][1][0]: 17
A[0][1][1]: 18
A[1][0][0]: 33
A[1][1][0]: 49
A[1][1][1]: 50
A[1][2][0]: 150
[chuck](VM): ArrayOutofBounds: in shred[id=1:3D matrix tests.ck], PC=[896], index=[2]
|
Any thoughts? Conceptually, your description of how multidimensional arrays work is making sense to me. But the implementation you suggested isn't working (unless I'm missing something). |
|