1

Is there a way to convert a string into a multi-dimensional array?

e.g.:

new Array(new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), new
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new 
    Array(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new 
    Array(1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,0,1), new 
    Array(1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,1,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1), new 
    Array(1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new 
    Array(1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new 
    Array(1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new 
    Array(1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new 
    Array(1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new 
    Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))

It's a string, the words and the brackets are characters too, I want to convert this into a real Array.

5
  • what are you trying to do exactly? Commented Aug 6, 2012 at 17:04
  • are you trying to do some kind of "eval" ? Commented Aug 6, 2012 at 17:06
  • Yes, I'm trying to convert one of the textfield's text (where I insert the above array) into a multi-dimensional array. (the same array structure) Commented Aug 6, 2012 at 17:18
  • 1
    This seems terribly complicated, and weird, what are you "really" trying to do? :0 Commented Aug 6, 2012 at 17:23
  • Input Text Field -> Multi Dimensional Array Commented Aug 6, 2012 at 17:33

1 Answer 1

2

I am sure there is a better way to write the regex, but here it goes, basically it removes the "new", replaces "Array(" with "[" and replaces ")" with "]". The resulting string is a valid JSON string which can be turned into an array with JSON.parse().

var str:String = "new Array(new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), new\
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new \
    Array(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new \
    Array(1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,0,1), new \
    Array(1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,1,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1), new \
    Array(1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new \
    Array(1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new \
    Array(1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new \
    Array(1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new \
    Array(1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1), new \
    Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))";

    var myPattern:RegExp = /new/g;  

    var json:String = str.replace(myPattern, " ");   
    myPattern = /Array\(/g;

    json = json.replace(myPattern, "[");

    myPattern = /\)/g;
    json = json.replace(myPattern, "]");

    var array:Array = JSON.parse(json) as Array;
Sign up to request clarification or add additional context in comments.

2 Comments

My JSON (latest version) doesn't have parse function.
If you are using the Adobe libraries then use JSON.decode() JSON.parse is available in the flash player after version 11.

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.