Eval new array from string

So here is my problem:

I am passing flash a variable in string format and it looks like this

var stringVar = “new Array(new Array(1,3,4),new Array())”;

Now i need to make an array out of it. I tried this:

var stringArray = eval(stringVar);

but had no success.

Is there a way to do it? If yes, then how?

It would be fairly simple in AS3 with regexp but in AS2 it’s going to be a lot of splitting, substringing and screwing around. You’re just gonna have to keep parsing it with the String methods until you get what you want.

If that would be easy, i wouldn’t be posting here :), but ok, then i know. Thanks.

I never said it would be easy :stuck_out_tongue:

There might be someone willing to figure it out for you but I’m not. At least not right now :slight_smile:

var stringVar:String = "new Array(new Array(1,3,4),new Array())";
var stringArray:Array = stringVar.substring(stringVar.indexOf(",")-1,stringVar.lastIndexOf(",")-1).split(",");
for (var i = 0; i<stringArray.length; i++) {
    trace(stringArray*);
}

Of course, it would be a whole lot easier if you simply passed in the elements of your array rather than trying to nest all the new Array text around it first:


var stringVar:String = "1,3,4";
var stringArray:Array = stringVar.split(",");
for (var i = 0; i<stringArray.length; i++) {
    trace(stringArray*);
}