Search code examples
actionscript-3flashflash-cc

How can I create 2D array dynamically?


I'm trying to create 2d array however I'm getting error. Here's my code:

var cleanArr:Array = new Array();
for (var i:int = 0; i < arr.length; i++)
{
    cleanArr[i][0] = arr[i].substring(0, 29);
    cleanArr[i][1] = arr[i].substring(29, int.MAX_VALUE);

    trace(cleanArr[i]);
}

I get this error:

TypeError: Error #1010: A term is undefined and has no properties.

at SubtitleLoader/onComplete()[C:\Users\ ... \SubtitleLoader.as:88]

at flash.events::EventDispatcher/dispatchEventFunction()

at flash.events::EventDispatcher/dispatchEvent()

at flash.net::URLLoader/onComplete()

Line 88: cleanArr[i][0] = arr[i].substring(0, 29);

How can I fix this?


Solution

  • cleanArr is completely empty, it isn't a 2D array. For it to be a 2D Array, the items in it have to be Arrays. In Your code they do not exist at all. You have to create an array at the given position first. Try this:

    var cleanArr:Array = new Array();
    for (var i:int = 0; i < arr.length; i++)
    {
        cleanArr[i] = new Array();
        cleanArr[i][0] = arr[i].substring(0, 29);
        cleanArr[i][1] = arr[i].substring(29, int.MAX_VALUE);
    
        trace(cleanArr[i]);
    }