# For loop variables/arrays

**URL:** <https://forum.kirupa.com/t/for-loop-variables-arrays/7781>\
**Category:** Uncategorized\
**Created:** [November 7, 2002, 4:42pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781 "2002-11-07T16:42:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iammontoya](https://avatars.discourse-cdn.com/v4/letter/i/bbce88/32.png) [@Iammontoya](https://forum.kirupa.com/u/Iammontoya)\
**Post date:** [November 7, 2002, 4:42pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/1 "2002-11-07T16:42:37Z")

</div>

in a for loop, I want to assign new arrays and possibly their initial value as well… I can do the values without problem, the array name has turned out to be tricky for me.

Example:

for(i=1;i\<10;i++){

myarray\*=[this.myvariable\*];  
}

with the intent of getting

myarray1=myvariable1;  
myarray2=myvariable2;  
myarray3=myvariable3;

I know Im missing something, just cant think straight.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 4:52pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/2 "2002-11-07T16:52:40Z")

</div>

have you done a:

myArray=new Array(10);

???

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 5:00pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/3 "2002-11-07T17:00:13Z")

</div>

```php
// Intialize the Array
myarray = new Array();
for (var i=0; i<10; i++) {
        myarray* = this.myvariable*;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 5:01pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/4 "2002-11-07T17:01:01Z")

</div>

> \*Originally posted by Dravos \*  
> \*\*have you done a:

myArray=new Array(10);

??? \*\*

huh? didnt c ur post!!!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 6:50pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/5 "2002-11-07T18:50:05Z")

</div>

OK h88 seems u pick up on everything !! so if i want to do it in your php stylee i just click php then type and then click php to end?

```php
//my code stylee stuff;
myArray=newArray(10)

```

better?

nope still dont work dammit…have to disable them smilis then, is there another way to get rid of the smiley when you do 0) ??

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 6:54pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/6 "2002-11-07T18:54:45Z")

</div>

I’ll give it a shot too 😛

```auto
for(i=1;i < 10;i++){
  myarray*=this["myvariable"i];
}

```

pom :cowboy:

Long time, Ivan… preparing something?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 6:58pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/7 "2002-11-07T18:58:03Z")

</div>

Hum, actually, this surprises me a bit:

> myarray1=myvariable1;  
> myarray2=myvariable2;  
> myarray3=myvariable3;  
> I mean, myarray is an array, right?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 7:44pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/8 "2002-11-07T19:44:19Z")

</div>

> \*Originally posted by ilyaslamasse \*  
> \*\*Hum, actually, this surprises me a bit:I mean, myarray is an array, right? \*\*

offcourse, from its name!!!😛

yours,  
h88

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 7:56pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/9 "2002-11-07T19:56:57Z")

</div>

i have to agree with ilyaslamasse

looks like it supposed to be

for(i=1;i\<10;i++)  
{  
[“myArray”+i]=[“myVariable”+i];  
}

but then again it could be

myArray[1]=myVariable[1]  
myArray[2]=myVariable[2]  
myArray[3]=myVariable[3]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 8:16pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/10 "2002-11-07T20:16:08Z")

</div>

> \*Originally posted by Dravos \*  
> \*\*i have to agree with ilyaslamasse

looks like it supposed to be

for(i=1;i\<10;i++)  
{  
[“myArray”+i]=[“myVariable”+i];  
}

but then again it could be

myArray[1]=myVariable[1]  
myArray[2]=myVariable[2]  
myArray[3]=myVariable[3] \*\*

Output ---------------  
Variable \_level0.i = 10  
Variable \_level0.my0 = 0  
Variable \_level0.my1 = 1  
Variable \_level0.my2 = 2  
Variable \_level0.my3 = 3  
Variable \_level0.my4 = 4  
Variable \_level0.my5 = 5  
Variable \_level0.my6 = 6  
Variable \_level0.my7 = 7  
Variable \_level0.my8 = 8  
Variable \_level0.my9 = 9

thats wrong, cuz it will just create some normal variables :evil:

yours,  
h88

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 8:17pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/11 "2002-11-07T20:17:33Z")

</div>

within the [] brackets is alwasy the index of the element of the array

[] have some more use. but you cant use them without an object in front of them:

some alternatives:

```php
varname="myvar";
_root[varname] = new Array(); 
for (var i=0; i<10; i++) { 
_root[varname]* = this.myvariable*; 
}

```

and

```php
for(var i=0; i<10; i++) {
     eval("myvar"+i) = this.myvariable*;
}

```

hope this helps!

yours,  
h88

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 10:45pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/12 "2002-11-07T22:45:06Z")

</div>

assuming you want i number of arrays each containing eval(myVariable+i) (as opposed to myVariable\*) as their first element, something like this:

```auto

for(i=0;i<10;i++){
   this["myArray"+i] = [this["myVariable"+i]];
}

```

but as everyone seems to be, i’m a little confused as to just what you’re trying to accomplish.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2002, 11:13pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/13 "2002-11-07T23:13:31Z")

</div>

> \*Originally posted by sbeener \*  
> \*_assuming you want i number of arrays each containing eval(myVariable+i) (as opposed to myVariable_) as their first element, something like this:

```auto

for(i=0;i<10;i++){
   this["myArray"+i] = [this["myVariable"+i]];
}

```

but as everyone seems to be, i’m a little confused as to just what you’re trying to accomplish. \*\*

ask Iammontoya, i was just helping in coding, correcting the syntax, i have no idea wut he wants to achieve?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2002, 12:07am UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/14 "2002-11-08T00:07:47Z")

</div>

i was asking inigo. 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2002, 8:13am UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/15 "2002-11-08T08:13:54Z")

</div>

The whole point is: did Inigo really mean **myarray1** (variable, not an array, hence my question :)) or did he mean **myarray[1]** (an array this time).

pom 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2002, 8:15am UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/16 "2002-11-08T08:15:05Z")

</div>

hmm, i really dunno :q::q:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2002, 2:36pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/17 "2002-11-08T14:36:19Z")

</div>

Whew… thank you all for your replies.

What I actually want to do is this:

I am loading a bunch of variables from a text file. The text variables are in the form

variable1=value1,value2,value3&variable2=value1,value2,value3

In order to use the values for each variable I have to split them and put them in arrays.

```auto

 myarray=variable1.split(",");   

```

since I am loading several variables, I need to create a number of arrays that is equal to the number variables I have. At the same time, I was populating such arrays with the split value of the variable.

Hence…

```auto

for(i=1;i<10;i++){//where 10 is the number of variables loaded

myarray*=[this.myvariable*]; /* This actually is "myarray"+i , but I know that doesn't work. The part to the right of the assignment works fine and it is meant to circumvent to problem of "myvariable"+i    
*/
}

```

this was all meant to produce something like this…

myarray1=myvariable1.split(","); //each one is a different array  
myarray2=myvariable2.split(",");  
myarray3=myvariable3.split(",");

```auto

I somehow knew Supra would come out of hiding on this one. Thank's to all for your responses!

Pom, I've been so busy I just had to post this one, even though I am like you (and others on this board), I like to figure these out on my own, it's just part of the fun. However, I don't have the time this time around. So, your help (everyone's help) is greatly appreciated.
```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2002, 2:38pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/18 "2002-11-08T14:38:35Z")

</div>

btw, Supra… your answer makes sense to me. I will try in a few minutes.

Pom, what’s up with you, man. How have you been?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2002, 9:15pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/19 "2002-11-08T21:15:15Z")

</div>

based on all the above threads…

eval(“myarray”+i)=etc… does not work. Left of the assignment must be variable.

this[“myarray”+i]= does not work for the same reason

still looking for any takers. Now, of course, by adding the .split mentioned above, things get creepy! heheheh…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 9, 2002, 1:08pm UTC](https://forum.kirupa.com/t/for-loop-variables-arrays/7781/20 "2002-11-09T13:08:34Z")

</div>

Oh, nothing very interesting. I’ve just finished my semester, had my exams… I’m going to Viet-Nam in a few days :beam:

Concerning your problem, could you post all the code you are using?

Cheers.

pom :asian:

[Next page](https://forum.kirupa.com/t/for-loop-variables-arrays/7781.md?page=2)
