Old 12-12-2011, 12:14 PM   #1
Erriez
Human being with feelings
 
Join Date: Jun 2010
Location: The Netherlands
Posts: 177
Default How to define a multidimension array

Hi all,

I'd like to define a multidimension array as well as normal arrays in one single JS script. The syntax in C is:

Code:
int array1[50][64];
int array2[16];
I can't find it in the documentation how to do this. Any suggestions? Thanks!
Erriez is offline   Reply With Quote
Old 12-12-2011, 02:57 PM   #2
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,645
Default

I just did a quick forum search for "array" in the JS subforum, and this topic seems relevant:
http://forum.cockos.com/showthread.php?t=79621
Tale is offline   Reply With Quote
Old 12-12-2011, 03:09 PM   #3
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 15,750
Default

What Mich suggests in the other thread is the right way to do it. If you want a 64*16 array, then write arr[i*16+j] instead of arr[i][j].
schwa is offline   Reply With Quote
Old 12-13-2011, 12:14 PM   #4
Erriez
Human being with feelings
 
Join Date: Jun 2010
Location: The Netherlands
Posts: 177
Default

I saw the other thread, but I had a different question: I'd like to define multiple normal arrays mixed with multidimension arrays. After spending a lot of time to reinvent the wheel about this undocumented stuff, I found a solution.

My problem was located in the definition of the arrays which is completely different than I expected. Please find below a full working example with a memory verification:

Code:
errorsArray1 = 0;
errorsArray2 = 0;
errorsArray3 = 0;

// C style: int array1[3];
array1 = 3;
// C style: int array2[2][3];
array2 = (array1 + (2*3));
// C style: int array3[4];
array3 = (array1 + array2 + 4);

// Initialize arrays
array1[0] = 11;
array1[1] = 12;
array1[2] = 13;

array2[(0*3)+0] = 14;
array2[(0*3)+1] = 15;
array2[(0*3)+2] = 16;
array2[(1*3)+0] = 17;
array2[(1*3)+1] = 18;
array2[(1*3)+2] = 19;

array3[0] = 20;
array3[1] = 21;
array3[2] = 22;
array3[3] = 23;

// Verify arrays
(array1[0] != 11) ? errorsArray1 += 1;
(array1[1] != 12) ? errorsArray1 += 1;
(array1[2] != 13) ? errorsArray1 += 1;

(array2[(0*3)+0] != 14) ? errorsArray2 += 1;
(array2[(0*3)+1] != 15) ? errorsArray2 += 1;
(array2[(0*3)+2] != 16) ? errorsArray2 += 1;
(array2[(1*3)+0] != 17) ? errorsArray2 += 1;
(array2[(1*3)+1] != 18) ? errorsArray2 += 1;
(array2[(1*3)+2] != 19) ? errorsArray2 += 1;

(array3[0] != 20) ? errorsArray3 += 1;
(array3[1] != 21) ? errorsArray3 += 1;
(array3[2] != 22) ? errorsArray3 += 1;
(array3[3] != 23) ? errorsArray3 += 1;
Output: Variables errorsArray1, errorsArray2 and errorsArray3 are 0 which is OK.

Can someone please update the outdated page below?
http://www.reaper.fm/sdk/js/js.php
Arrays are the basics of programming! (Sorry for my frustration about this)

Thanks for your help!

Last edited by Erriez; 12-13-2011 at 12:55 PM.
Erriez is offline   Reply With Quote
Old 12-13-2011, 04:23 PM   #5
Mich
Human being with feelings
 
Join Date: May 2009
Posts: 1,265
Default

Quote:
Originally Posted by Erriez View Post
Can someone please update the outdated page below?
http://www.reaper.fm/sdk/js/js.php
Arrays are the basics of programming! (Sorry for my frustration about this)
What's outdated about the documentation?

Also grep for "array" in the documentation you will not find it. Because [] in JS designates a memory access and no array access. How the memory access works is clearly outlined in the documentation, no?

Just because JS uses squared brackets doesn't mean their semantic should be the same as in some other language! Squared brackets in JS access the memory at the position calculated from the value before the [] (the offset) plus the value within the [] (the index).

So in conclusion their is no array syntax in JS, hence no arrays.
__________________
Quote:
Originally Posted by vBulletin Message
Sorry pipelineaudio is a moderator/admin and you are not allowed to ignore him or her.
Mich is offline   Reply With Quote
Old 12-14-2011, 12:18 PM   #6
Erriez
Human being with feelings
 
Join Date: Jun 2010
Location: The Netherlands
Posts: 177
Default

The only documentation I found is this link, or do I miss something?

Quote:
Just because JS uses squared brackets doesn't mean their semantic should be the same as in some other language!
I agree, but it should be clearly documented and it's not.

Quote:
How the memory access works is clearly outlined in the documentation, no?
No. The memory access usage doc is maybe clear for you, but not for me:

Quote:
Chapter [ ]
Example: z=x[y]; or x[y]=z;
Example: z=gmem[y]; or gmem[y]=z;
In the first form, you may use brackets to index into memory that is local to your effect. Your effect has approximately 8 million (8,388,608) slots of memory and you may access them either with fixed offsets (i.e. 16811[0]) or with variables (myBuffer[5]). If a value in the brackets is omitted then 0 is used instead.

If 'gmem' is specified (the second form), then instead of local effect memory, the buffer is the global storage buffer, which is approximately 1 million (1,048,576) slots that are shared across all effects.
A simple real practical example is missing in this chapter. I discussed this theoretical text with two independent senior C programmers, but we don't understand it. I've wasted lots of hours debugging to find the right answer, but its still undefined behaviour for me.

Quote:
Squared brackets in JS access the memory at the position calculated from the value before the [] (the offset) plus the value within the [] (the index).
Sorry, we can't follow you: "at the position calculated from the value before the [] (the offset)" What do you mean?

Reaper developers, can you please change this chapter to human readable/practical documentation and spend a few words about 'arrays'? Reaper is made for musicians... not only for advanced programmers of a self-invented script language.
Erriez is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.