# Accessing many array variables in a for loop

**URL:** <https://forum.kirupa.com/t/accessing-many-array-variables-in-a-for-loop/307373>\
**Category:** Uncategorized\
**Created:** [April 2, 2010, 8:56am UTC](https://forum.kirupa.com/t/accessing-many-array-variables-in-a-for-loop/307373 "2010-04-02T08:56:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceherosubzero](https://avatars.discourse-cdn.com/v4/letter/i/8797f3/32.png) [@iceherosubzero](https://forum.kirupa.com/u/iceherosubzero)\
**Post date:** [April 2, 2010, 8:56am UTC](https://forum.kirupa.com/t/accessing-many-array-variables-in-a-for-loop/307373/1 "2010-04-02T08:56:57Z")

</div>

hello all,  
can any body tell me how can I access my different array variables in a for loop?  
what i need to do is for every increment in my for loop i want to change my var array name so that i don’t need to use if else condition.

```auto

var ship_array:Array = new Array("spaceship1", "spaceship2", "spaceship3", "spaceship4", "spaceship5", "spaceship6", "spaceship7", "spaceship8", "spaceship9", "spaceship10", "spaceship11", "spaceship12", "spaceship13", "spaceship14", "spaceship15", "spaceship16", "spaceship17", "spaceship18", "spaceship19", "spaceship20", "spaceship21", "spaceship22", "spaceship23", "spaceship24");
var spaceships:Array = new Array("sp1", "sp2", "sp3", "sp4", "sp5", "sp6", "sp7", "sp8", "sp9", "sp10", "sp11", "sp12", "sp13", "sp14", "sp15", "sp16", "sp17", "sp18", "sp19", "sp20", "sp21", "sp22", "sp23", "sp24");
//---// VD1 VD2 VD3 for spaceship 1 //---//
var spaceship1_VD1:Array = new Array("spaceship5", "spaceship15", "spaceship17");
var spaceship1_VD2:Array = new Array("spaceship4", "spaceship10", "spaceship13", "spaceship14", "spaceship21");
var spaceship1_VD3:Array = new Array("spaceship2", "spaceship3", "spaceship6", "spaceship7", "spaceship8", "spaceship9", "spaceship11", "spaceship12", "spaceship16", "spaceship18", "spaceship19", "spaceship20", "spaceship22", "spaceship23", "spaceship24");

//---// VD1 VD2 VD3 for spaceship 2 //---//
var spaceship2_VD1:Array = new Array("spaceship7", "spaceship18", "spaceship11");
var spaceship2_VD2:Array = new Array("spaceship6", "spaceship14", "spaceship13", "spaceship16", "spaceship21");
var spaceship2_VD3:Array = new Array("spaceship2", "spaceship3", "spaceship6", "spaceship7", "spaceship8", "spaceship9", "spaceship10", "spaceship11", "spaceship16", "spaceship17", "spaceship19", "spaceship20", "spaceship21", "spaceship23", "spaceship24");

var Selections:Array = new Array();
var targetSpaceship:Array = new Array();
for (var l = 0; l<1; l++) {
	var selected_ship:Number = Math.floor(Math.random()*(ship_array.length-0)+0);
	//trace("SS"+selected_ship)
	//trace("SA "+ship_array[selected_ship]);
	Selections.push(ship_array[selected_ship]);
	targetSpaceship.push(spaceships[selected_ship]);
	//trace("TS "+targetSpaceship)
	//trace("Select "+Selections[0])
	if (Selections[0]=="spaceship1") {
		var selected_VD1:Number = Math.floor(Math.random()*(spaceship1_VD1.length-0)+0);
		Selections.push(spaceship1_VD1[selected_VD1]);
		var selected_VD2:Number = Math.floor(Math.random()*(spaceship1_VD2.length-0)+0);
		Selections.push(spaceship1_VD2[selected_VD2]);
		var selected_VD3:Number = Math.floor(Math.random()*(spaceship1_VD3.length-0)+0);
		Selections.push(spaceship1_VD3[selected_VD3]);
		trace("selections "+Selections);
		trace("TS 1 "+targetSpaceship)
	} 
}

```

is there any way to change my spaceship1\_VD1, spaceship1\_VD2, and spaceship1\_VD3 names to spaceship2\_VD1, spaceship2\_VD2 and spaceship2\_VD3 and so on. can i change the 1 in my var spaceship1\_VD1 to spaceship2\_VD1 where both the var are my arrays.  
and also in my if condition (Selections[0]==“spaceship1”) can i pass a number var which will change and it can be like “spaceship”+that number so i can get spaceship1, spaceship2, and so on…

plz help  
thank you  
iceheros
