|
-
[F8] Adding a multidimensional array together?
I've got a m.d array which I am filling with values. Is there a quick way to add up every item? Maybe a function built in?
-
Senior Member
Code:
var p:Array = [3, 4, 5, [5, 3, 1, [2, 3], 3], 34];
function addValues(array:Array):Number {
var toReturn:Number = 0;
function getNums(array:Array) {
for (var z in array) {
(array[z] instanceof Array) ? getNums(array[z]) : (typeof(array[z]) == "number") ? toReturn += array[z] : null;
}
}
getNums(array);
return toReturn;
}
trace(addValues(p))
-
Can I copy and paste this code or do I have to modify it??
-
Senior Member
copy and paste the function, the array "p" is just there as an example.
zlatan
-
It's not working - I get an 'undefined' error.
My array is like this:
array[0][0] = 1
array[0][1] = 2
array[3][4] = 5
etc...
-
Senior Member
it is you code that is at fault, not mine.
you can not do array[0][0] = 0 unless it is defined before hand, i.e.
if you do :
Code:
var array:Array = new Array();
array[0][0] = 0;
trace(array[0][0]);
it will trace undefined, you have to do it like this:
Code:
var array:Array = new Array();
array[0] = [1];
trace(array[0][0]);
or a similar method,
HTH,
zlatan
-
This is what the array is done with:
Code:
aWeek = new Array();
for(i=0;i<7;i++){
aWeek[i] = new Array();
for(ii=0;ii<12;ii++){
aWeek[i][ii] = "";
}
}
I thought this would be better - or at least similar to what you are advising me to do:
Code:
function addArrays(array, v){
var arr1:Number = array.length;
for(i=0;i<arr1;i++){
var arr2:Number = array[i].length;
for(ii=0;ii<12;ii++){
v = v+parseInt(array[i][ii]);
//trace(v);
}
}
}
But this does not show anything:
Code:
addArrays(wProtein, vProtein);
trace(vProtein);
It just shows vProtein as 0, so it's not getting updated. ... Why could that be?
-
Senior Member
as i said, you have to define the nested array first, this would be the right way to set up your array:
Code:
var aWeek:Array = new Array();
for(var z = 0; z<7; z++){
var temp:Array = new Array();
for(var j = 0; j<12; j++){
temp.push("");
}
aWeek.push(temp);
}
zlatan
-
Thanks for your help so far, you've put me in the right direction. This is how I solved this:
Code:
vProtein = addArrays(wProtein, vProtein);
What I did was to change the addArray function so it would return the v value and then I put that into vProtein. I was getting a weird error at first, the values were adding on and accumulating so I changed the addArray function:
Code:
function addArrays(array, v){
v=0; <-------------------------- I added this line
var arr1:Number = array.length;
for(i=0;i<arr1;i++){
var arr2:Number = array[i].length;
for(ii=0;ii<12;ii++){
v = v+parseInt(array[i][ii]);
//trace(v);
}
}
return v;
}
So now the array adds to v from scratch and everything works great. Maybe this will have an unspotted error but to be honest I have tested it and it seems fine!
Thanks again for making me think harder!
-
Senior Member
thats cool, but you dont need the "v" parameter in the addArrays() function.
glad it works,
zlatan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|