1

Ok! I have a flashVar variable that is coming into Flash, its URL encoded but I have already decoded it. My problem is I want the set of variables to be pushed into an array.

Let's say the variables are

"&text0=Enter Text...&size0=18&font0=Arial&color0=0&rotation0=0&y0=360&x0=640&text1=Enter Text...&size1=18&font1=Arial&color1=0&rotation1=0&y1=360&x1=640"

and so on...

What I want is the variables to go into an array like

myArray[0].text = Enter Text...
myArray[0].size = 18]
myArray[0].font = Arial
myArray[0].color = 0
myArray[0].rotation = 0
myArray[0].y = 360
myArray[0].x = 640
myArray[1].text = ...........
.............................
.............................
myArray[n].text = ...........

I think there must be some way to do this. Most probably I'm thinking regular expression, but I'm pretty bad at regular expression. Please some help would be very very appreciated.

Thank You!

3 Answers 3

3

You don't have to decode your query string, just use the URLVariables object - it will do all the decoding for you. Then iterate over its dynamic properties to create your array. Use a RegExp to find the index numbers at the end of your variable keys:

function parseURLVariables( query:String ) : Array {
    var vars:URLVariables = new URLVariables (query);
    var arr:Array = [];
    for (var key : String in vars) {
        var splitIndex : int = key.search(/[0-9]+$/);
        var name:String = key.substr (0,splitIndex);
        var indexNumber:int = parseInt ( key.substr(splitIndex));
        arr[indexNumber] ||= {};
        arr[indexNumber][name] = vars[key];
    }
    return arr; 
}

Since your query string starts with a an ampersand, you might have to use parseURLVariables ( myString.substr(1)), otherwise the URLVariables object will throw an error, complaining that the query string is not valid (it has to be url encoded, and start with a variable key).

Sign up to request clarification or add additional context in comments.

Comments

0

you may use split method of string to something like this;

var astrKeyValue: Array = url.Split( "&" );

in this way each value in astrKeyValue is string keyvalue ( for example font1=Arial ) after than you may split each item with "=" and will get pair key and value ( for key - font1 and for value - arial) so this code maybe will work for you

var str = "text0=Enter Text...&size0=18&font0=Arial&color0=0&rotation0=0&y0=360&x0=640&text1=Enter Text...&size1=18&font1=Arial&color1=0&rotation1=0&y1=360&x1=640"
var a : Array = str.split( "&" );
var newArr: Array = new Array()
for each ( var str1 in a )
{
    var t: Array = str1.split( "=" );
    newArr[ t[0] ] = t[1];
}

trace( newArr.text0 ) // -> Enter Text...

1 Comment

You should look at the URLVariables class - it might save you some time in the future. Also: OP wants to parse these values into an indexed array - text0 will not suffice. You should read the questions more carefully.
0

Here is a solution for you from me,

//your string data should be like this, there should be a seperate seperator (i've used pipe sign |) for each element which will be converted to an object and then pushed to the array
var strData:String = "text=Enter Text...&size=18&font=Arial&color=0&rotation=0&y=360&x=640|text=Enter Text...&size=18&font=Arial&color=0&rotation=0&y=360&x=640";

var myArray:Array = new Array();
var _tmpArr:Array = strData.split("|");

//populating the array
for(var i:int=0;i<_tmpArr.length;i++)
{
    myArray.push(strToObj(_tmpArr[i]));
}
trace(myArray.length);

// coverts chunk of string to object with all key and value in it
function strToObj(str:String):Object
{
    var obj:Object = new Object();
    var tmpArr:Array = str.split('&');

    for (var i:int = 0; i < tmpArr.length; i++)
    {
        var _arr:Array = String(tmpArr[i]).split('=');
        var key:String = String(_arr[0]);
        var val:String = String(_arr[1]);
        obj[key] = val;
        trace(key+" = "+val);
    }
    trace("----");
    return obj;
}

2 Comments

Dude, I'm not allowed to used "|" separators.
That was biggest problem in the example. To split the strings into arrays without having a separator to split it with, @weltraumpirat's function works. Thanks for taking the time to help.

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.