A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [F8] Adding a multidimensional array together?

  1. #1
    Senior Member
    Join Date
    May 2004
    Posts
    322

    [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?
    Everybody has a story to tell, what's yours? -- http://www.filmfiler.com

  2. #2
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    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))
    New sig soon

  3. #3
    Senior Member
    Join Date
    May 2004
    Posts
    322
    Can I copy and paste this code or do I have to modify it??
    Everybody has a story to tell, what's yours? -- http://www.filmfiler.com

  4. #4
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    copy and paste the function, the array "p" is just there as an example.

    zlatan
    New sig soon

  5. #5
    Senior Member
    Join Date
    May 2004
    Posts
    322
    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...
    Everybody has a story to tell, what's yours? -- http://www.filmfiler.com

  6. #6
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    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
    New sig soon

  7. #7
    Senior Member
    Join Date
    May 2004
    Posts
    322
    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?
    Everybody has a story to tell, what's yours? -- http://www.filmfiler.com

  8. #8
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    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
    New sig soon

  9. #9
    Senior Member
    Join Date
    May 2004
    Posts
    322
    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!
    Everybody has a story to tell, what's yours? -- http://www.filmfiler.com

  10. #10
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    thats cool, but you dont need the "v" parameter in the addArrays() function.

    glad it works,

    zlatan
    New sig soon

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center