• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

How do I push 0[i] into an array

I cant figure out how to push a series of zeroes into an array.
What I want is to populate the array with zeroes if the array is empty, which it will be the first time of the iteration.

for(i = 0; i < colorList.length; i++){
var statList:Array=[];
if(statList.length>=1){
statList=new Array();
statList=statArray;
}
else{
statList=new Array(i);
statList[i]=0;
}
}
But my trace only results in this 
//,,,,,,,,,0
there is 10 items in colorList

Open in new window

0
petersego
Asked:
petersego
  • 4
  • 3
1 Solution
 
dgofmanCommented:
Are you trying yo create a double array metrix?

I don't see you are pushing statList into colorList.

What is your logic?
0
 
petersegoAuthor Commented:
Im trying to use the length from colorList to create a status for each of a series of movies.
like this.
statArray=statList
myMovie.status = statArray[i];

Open in new window

eventually statArray will be changed from a lot of zeroes to fx. 0,0,0,1,0,0,1,1,0,0


0
 
dgofmanCommented:
I am still confusing with your idea but let's start with this changes first

var colorList:Array = new Array(10);
var statList:Array;

for(var i:uint = 0; i < colorList.length; i++){
	if(!(colorList[i] is Array)){
		colorList[i] = new Array();
	}
	statList = colorList[i];
	statList.push(0);
}

//Test if we can append a new value
for(i = 0; i < colorList.length; i++){
	statList = colorList[i];
	statList.push(1);
}

//Print all values
for(i = 0; i < colorList.length; i++){
	trace("Index: " + i + ", values: " + colorList[i]);
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dgofmanCommented:
or

var colorList:Array = new Array(10);

function addValue(value:uint):void{
	for(var i:uint = 0; i < colorList.length; i++){
		if(!(colorList[i] is Array)){
			colorList[i] = new Array();
		}
		var statList:Array = colorList[i];
		statList.push(value);
	}
}

//Test if we can append a new value
addValue(0);
addValue(0);
addValue(0);
addValue(1);
addValue(1);
addValue(0);
addValue(0);

//Print all values
for(var i:uint = 0; i < colorList.length; i++){
	trace("Index: " + i + ", values: " + colorList[i]);
}

Open in new window

0
 
petersegoAuthor Commented:
I think you are on the right track, but the trace for the last example results in this:
Index: 0, values: 0,0,0,1,1,0,0
Index: 1, values: 0,0,0,1,1,0,0
Index: 2, values: 0,0,0,1,1,0,0
Index: 3, values: 0,0,0,1,1,0,0
Index: 4, values: 0,0,0,1,1,0,0
Index: 5, values: 0,0,0,1,1,0,0
Index: 6, values: 0,0,0,1,1,0,0
Index: 7, values: 0,0,0,1,1,0,0
Index: 8, values: 0,0,0,1,1,0,0
Index: 9, values: 0,0,0,1,1,0,0

If I for the last part replace colorList with statList it results in this
for(var i:uint = 0; i < colorList.length; i++){
	trace("Index: " + i + ", values: " + statList[i]);
}

Open in new window

Index: 0, values: 0
Index: 1, values: 0
Index: 2, values: 0
Index: 3, values: 1
Index: 4, values: 1
Index: 5, values: 0
Index: 6, values: 0
Index: 7, values: undefined
Index: 8, values: undefined
Index: 9, values: undefined

0
 
petersegoAuthor Commented:
This has solved the problem, so if its alright with you, Ill close the question

statList=[];
if(statArray.length>=1){
statList=new Array();
statList=statArray;
}
else{
statList=new Array();
for each(var p:Number in colorList){
statList.push(0);
}
}

Open in new window

0
 
dgofmanCommented:
Thank you for feedback but I didn't get points from you :(
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now