# Problem with arrays

• 11-30-2013, 08:11 PM
sotnas93
Problem with arrays
Hi, I'm trying to create a function that saves a vector that represents time, or several instances of time in this case, and for each instance it holds a vector of several positions (x,y) of several objects. For some reason when I run it the first alert gives me the results as I want them but then of the last alerts it just gives me same result (on the 3) and is the last one that run through the while. It's like it's only saving the last entry. What am I doing wrong? Thank you and sorry for my bad English.

function randomNumbers(){
i=0;
while (i<=tRight-1){
w=0;
while(w<=Numb_lights-1){
x=Math.round(Math.random()*5)*100+307;
y=Math.round(Math.random()*2)*150+95;
valoresx[w]=x;
valoresy[w]=y;
w=w+1;
}
valores[0]=valoresx;
valores[1]=valoresy;
valorest=new Array();
valorest=valores;
);
i=i+1;
}
}
• 12-02-2013, 06:32 AM
tech_soul8
First, put your code inside of the code tag. Second, it would be helpful if you post the entire code.
• 12-02-2013, 08:26 AM
\\.\
If you have an array 'x' and you need to add a further dimension to it,
Code:

```c = 0; x = new Array(); x[c] = new Array();```
now if you increment the value of c in a loop, you will get x[n] arrays that are arrays in themselves.
Code:

```x = new Array(); for( c = 0; c<10; c++){     x[c] = new Array();     }```
So storing a value at a vector of 3,9 for example would be as simple as ...
Code:

`x[3][9] = 1;`
and if you needed to store a pair of numbers, you could either use an array or an object.

Array method...
Code:

`x[3][9] = new Array(1,2);`
Object method
Code:

`x[3][9] = {xvalue:1,yvalue:2};`
the advantage of an object method is you can name the elements and use the names of those elements like this
Code:

`yval = x[3][9].yvalue;`
in the array method you would need to know that x[3][9][1] was the y value or use associative array names like in an object like
Code:

```x[3][9] = new Array(); x[3][9]['xvalue'] = 1; x[3][9]['yvalue'] = 2;```
• 12-05-2013, 09:29 AM
sotnas93

Code:

```x_image = new Array();         for( c = 0; c<3; c++){             x_image[c] = new Array();     }         for( cc = 0; cc<25; cc++){                 valor_x=660+cc*154;                 for( ccc = 0; ccc<3; ccc++){                         valor_y=914+ccc*154;                             x_image[cc][ccc] = new Array();                         x_image[cc][ccc][0] = valor_x;                         x_image[cc][ccc][1] = valor_y;                 }     }```
but it is not working, it says "Cannot set property '0' of undefined " on the "x_image[cc][ccc] = new Array();" line. What I am doing wrong? O.o
• 12-05-2013, 10:46 AM
\\.\
You need to have something like this.

x_image = new Array();
for( c = 0; c<3; c++){
x_image[c] = new Array();
}
for( cc = 0; cc<25; cc++){
x_image[cc] = new Array();
valor_x=660+cc*154;
for( ccc = 0; ccc<3; ccc++){
valor_y=914+ccc*154;
x_image[cc][ccc] = new Array();
x_image[cc][ccc][0] = valor_x;
x_image[cc][ccc][1] = valor_y;
}
}

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11233 seconds
• Memory Usage 2,359KB
• Queries Executed 11 (?)
Template Usage (20):
• (8)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates