2

Im trying to make a multidimensional array but I obtain an error ("TypeError: Error #1010: A term is undefined and has no properties.").

var matriz:Array = new Array();
for(var p:Number = 0; p<2;p++ ){
    for(var q:Number = 0; q<2;q++ ){
        matriz[p][q] = 0;
    }
}

what am I doing wrong?

Thanks in advance!

3 Answers 3

3

You need to create an array within matriz[p] before you can add an array (or anything else) into it.

You can achieve what you're attempting without errors like this:

var matriz:Array = [];

for(var p:Number = 0; p<2; p++)
{
    // Create an array at matriz[p] if undefined.
    if(matriz[p] == undefined) matriz[p] = [];

    for(var q:Number = 0; q<2; q++)
    {
        matriz[p][q] = 0;
    }
}

Essentially you were trying to do the same as this:

var object:Object = {};
object.nonexistantProperty.value = 10;
Sign up to request clarification or add additional context in comments.

Comments

2

What Marty has said is correct, however I prefer removing the if condition and changing the code to the following:

var matriz:Array = [];

for(var p:Number = 0; p<2; p++) {
    matriz[p] = [];
    for(var q:Number = 0; q<2; q++) {
        matriz[p][q] = 0;
    }
}

6 Comments

Cleaner, but what if this code is run later down the track and matriz[p][q] = val; is changed to matriz[p].push(val);.
Sorry, I don't understand what you mean. It's just that I wanted to avoid having to run the comparison if(matriz[p] == undefined) p*q times. Other than that, I don't see the difference between my code and yours.
If the above was in a function or similar and matriz was used elsewhere, all the inner arrays would be reset every time it was run. I could probably move the comparison outward one level though to not have to compare so many times, good tip.
Ok, I see you point now :) But in that case, It depends on what he wants to do: My code resets the inner array as you said and your code will keep the old values which may probably lead to an error if the new inner arrays are smaller than the old ones. It really depends on what he wants to accomplish.
Thanks a lot for your answers. Both examples are very useful!
|
0
public class cArray
{

    private  var DIM1CAP:uint=0;
    private  var DIM2CAP:uint=1;
    private  var DIM3CAP:uint=2;
    private  var DIM4CAP:uint=3;
    private  var DIM5CAP:uint=4;

    public function cArray():void
    {
        // avoid the noid
    }

    // returns empty array of args.length dimensions
    // 1st argument is dim 1 capacity; 2nd is dim 2, etc.
    public function getArray ( ... args ):Array
    {
        var arr = new Array();

        if ( paramsNotValid(args) )
        {
            return null;
        }

        switch (args.length)
        {
            case 2:
                arr = get2DArray ( args[0], args[1] );
                break;

            case 3:
                arr = get3DArray ( args[0], args[1], args[2] );
                break;

            case 4:
                arr = get4DArray ( args[0], args[1], args[2], args[3] );
                break;

            case 5:
                arr = get5DArray ( args[0], args[1], args[2], args[3], args[4] );
                break;

            default:

                break;
        }



        return arr;
    }


    // returns empty 2d array of parameter specified capacity
    private function get2DArray ( _1stDimCapacity:uint, _2ndDimCapacity:uint ):Array
    {
        var arr2d:Array = [];
        var arr1d = new Array();

        for ( var i:uint=0; i<_1stDimCapacity; i++ )
        {
            arr1d[_2ndDimCapacity-1] = undefined;
            arr2d.push(arr1d);
            arr1d = new Array();
        }
        return arr2d;
    }

    // returns empty 3d array of parameter specified capacity
    private function get3DArray ( dim1Cap:uint, 
                                  dim2Cap:uint, 
                                  dim3Cap:uint ):Array 
    {
        var arr3d = new Array();

        for ( var i:uint=0; i<dim1Cap; i++ )
        {
            arr3d.push ( get2DArray ( dim2Cap, dim3Cap ) );
        }
        return arr3d;
    }

    // returns empty 4d array of parameter specified capacity
    private function get4DArray ( dim1Cap:uint, 
                                  dim2Cap:uint, 
                                  dim3Cap:uint,
                                  dim4Cap:uint):Array 
    {
        var arr4d = new Array();

        for ( var i:uint=0; i<dim1Cap; i++ )
        {
            arr4d.push ( get3DArray ( dim2Cap, dim3Cap, dim4Cap ) );
        }
        return arr4d;
    }

    // returns empty 5d array of parameter specified capacity
    private function get5DArray ( dim1Cap:uint, 
                                  dim2Cap:uint, 
                                  dim3Cap:uint,
                                  dim4Cap:uint,
                                  dim5Cap:uint):Array 
    {
        var arr5d = new Array();

        for ( var i:uint=0; i<dim1Cap; i++ )
        {
            arr5d.push ( get4DArray ( dim2Cap, dim3Cap, dim4Cap, dim5Cap ) );
        }
        return arr5d;
    }

    //////////////////////////////////////////////////////
    private function paramsNotValid ( args:Array ):Boolean
    {
        if ( args.length<2 || args.length>5 )
        {
            return true;
        }

        for ( var i:uint=0; i<args.length; i++ )
        {
            if ( ! ( args[i]>0 ) ) 
            {
                break;
            } 
        }

        if ( i < args.length )
        {
            return true;
        }
        return false;
    }

}

}

public class cMain extends MovieClip {

    var cArr:cArray = new cArray;


    public function cMain():void
    {
        var arr2d:Array;
        var arr3d:Array;
        var chessBrd_4d:Array; 
        var arr5d:Array;

        // capacity of 10 games; 150 moves/gm; white's mv or black's;
        // - piece positions; move in chess notation (index 32 of 
        // - last dimension); commentary (index 33 of last dimension) 
        chessBrd_4d = cArr.getArray(10,150,2,34);

        // adding data
        // - 4th game, 8th move, white's move, positions of pieces
        chessBrd_4d[3][7][0][0] = 'd8';
        chessBrd_4d[3][7][0][1] = 'b1';
        //             ...
        // - positions of pieces up to last one
        //             ...
        // - last piece pos
        chessBrd_4d[3][7][0][31] = 'captured'; 
        // - actual move in chess notation 
        chessBrd_4d[3][7][0][32] = 'nC4';
        // - annotation
        chessBrd_4d[3][7][0][33] = 'blocks b pawn, ' +
                                   'opens diag for c1 bishop, ' +
                                   'Justin Beiber is a putz, ' +
                                   'the president is liar'

        trace ( 'piece 0 is on square ' + chessBrd_4d[3][7][0][0]);
        trace ( 'piece 1 is on square ' +  chessBrd_4d[3][7][0][1]);
        trace ( ' ... ' )
        trace ( 'piece 31 has been ' + chessBrd_4d[3][7][0][31]);
        trace ( 'move: ' + chessBrd_4d[3][7][0][32]);
        trace ( chessBrd_4d[3][7][0][33]);
        /*
        trace results:
        piece 0 is on square d8
        piece 1 is on square b1
         ... 
        piece 31 has been captured
        move: nC4
        blocks b pawn, opens diag for c1 bishop, 
        Justin Beiber is a putz, the president is liar
        */  

        /*
        trace ( chessBrd_4d.length ); = 10
        trace ( chessBrd_4d [ 2 ].length ); = 150
        trace ( chessBrd_4d [ 2 ] [ 4 ].length ); = 2
        trace ( chessBrd_4d [ 2 ] [ 4 ] [ 1 ].length ); = 34
        trace ( chessBrd_4d [ 2 ] [ 4 ] [ 0 ] .length); = 34
        */

    }
}

2 Comments

Please, elaborate your answer.
sorry about that - here is commented replacement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.