declaring arrays

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
miki
Posts: 17
Joined: December 20th, 2004, 10:48 am
Location: London, UK
Contact:

Hi everyone - newbie here!

How do you declare a 2D array?

The only way I've found to do it is to define every element

e.g. for a [3][3] sized array:

Code: Select all

var myarray=[[0,0,0],[0,0,0],[0,0,0]]; //define array
myarray[1][2]=7 //set a value
but if I just do:

Code: Select all

var myarray; //declare array
myarray[1][2]=7 //set a value
it doesn't work.

Of course this isn't a problem for tiny arrays but I wanna make a big one!

Come to think of it, I can't even declare a 1D array...

Any ideas?

Miki
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

I'm no expert here but I'll tell you what I usually do.

I don't think you have to spell out what kind of array you want. you can just say:

var myArray = new Array(); //makes an empty array
myArray[0]= 7; //add a single value to the first slot in the array
myArray[1]= "Hello World"; //add some text
myArray[2]= [1,2,3]; //Add a vector (I think that's how you do a vector)

It's my understanding that an array can have any type of thing at each position. You can even have arrays that contain arrays.

Hope that makes sense!
miki
Posts: 17
Joined: December 20th, 2004, 10:48 am
Location: London, UK
Contact:

Thanks - that certainly does the trick for 1D arrays :)

For 2D arrays I've resorted to clever indexing* of a 1D array as follows:

if my array is of size (x,y)
I store the (i,j)th element at myarray[i+j*x]

So if I want a (5,7) size array, I'll store the element (4,3) at:
myarray[4+3*5]
=myarray[19]

Miki

*I'm sure there's a proper name for this... dunno what it is though
Shinjipierre
Posts: 36
Joined: December 4th, 2004, 10:10 am

For 2D arrays you could have just used ...

Code: Select all

var myArray = new Array(new Array());
myArray[12][2] = "jambon";
or

Code: Select all

var myArray = [[]];
myArray[12][2] = "jambon";
Isn't it easier ? ^^
miki
Posts: 17
Joined: December 20th, 2004, 10:48 am
Location: London, UK
Contact:

ah, that's perfect!

thank you!
User avatar
valoos
Posts: 5
Joined: February 24th, 2006, 12:05 pm
Location: Poznan POLAND

I don't know what is it like in CS3, but in AE7 proper syntax is:

Code: Select all

var myArray = new Array(new Array()); // or Array[[]];
myArray[[12],[2]] = "jabon"; //important bracket outside and comma inside
Post Reply