# Thread: Problem with arrays

1. Registered User
Join Date
Nov 2013
Posts
2

## 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;
}
}

2. First, put your code inside of the code tag. Second, it would be helpful if you post the entire code.

3. 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;```

4. Registered User
Join Date
Nov 2013
Posts
2
Thank you for your answers! I just did this:

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

5. 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;
}
}
Last edited by \\.\; 12-05-2013 at 10:48 AM.

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09825 seconds
• Memory Usage 2,881KB
• Queries Executed 15 (?)
Template Usage (33):
• (8)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (5)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (5)postbit
• (5)postbit_onlinestatus
• (5)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• 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_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete