Split textfield value in 2 parts and put each part in a var - actionscript-3

I have a textfield, that has a text value: France Paris. Now I need to know how I can take that string, cut it in 2 parts (France and Paris) and put those two parts in a var. So:
<textfield id="field1" text="France Paris">
and somewhere i should get
var1 = France;
var2 = Paris;
I know there is a split string command, but I'm not familiar with any of this stuff.

For this you could use the split() method on your String. This will split your String and put the values in an Array. You can use it like this:
var yourString:String = "France Paris";
var splitString:Array = yourString.split(" ");
var firstWord:String = splitString[0];
var secondWord:String = splitString[1];
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#split()

Related

Separate strings in a string with Actionscripts3

I'm trying to separate two part of a string, one is Title one is Value, RegExp is confused me. I need your help to solve this thanks
var pattern2:RegExp = new RegExp("TZ_NUM_ANSWER:Telegram code([0-9.-]+)");//TZ_NUM_ANSWER:Telegram code 32263
var data2:Object = pattern2.exec(response);
if (data2 != null && data2[1] != null)
{
var value2:Number = parseFloat(data2[1]);
trace("TZ_NUM_ANSWER " + value2);
txt_BUY1.text = String(value2);
}
Output:
TZ_NUM_ANSWER:Telegram code 32263
It must be:
"TZ_NUM_ANSWER:" "Telegram code 32263"
The result of split is an Array you can access to Array indexes and assign them to a variable.
var STR1:String = "TZ_NUM_ANSWER:Telegram code 32263";
var STR2:String;
var STR3:String;
trace(STR1.split(":"));
STR2 = STR1.split(":")[0];
STR3 = STR1.split(":")[1];
trace (STR2);
trace (STR3);
Result:
TZ_NUM_ANSWER
Telegram code 32263
Don't use RegEx for simple stuff. All you need is basic string methods:
response.split(":");

actionscript remove (concat?) sub-arrays

I have multiple sub-arrays in one huge array - MasterArray- meaning that the sub-arrays are already INSIDE the MasterArray. I would like to "fuse" all of those sub-arrays - to remove those [ ] square brackets.
I would like to avoid the "concat" method because the arrays are already inside the MasterArray. Is there a command/method how to do this?
Thank you.
var englandCities:Array = [London, Manchester, Leeds];
var franceCities:Array = [Paris, Orleans, Avignon];
var europeanCities:Array = [englandCities, franceCities];
I would like to point let's say...to "London" nested in the europeanCities array somehow.
After I try to trace it, it gives me "englandCities", which makes sense.
trace(europeanCities[0]);
// displays "englandCities"
// how can I make it display "London" ?
How can I make the europeanCities array to display "London" ?
I NEED TO REMOVE THOSE SQUARE BRACES from the "europeanCities" array WITHOUT using the concat() thingie...
OKAY let me rephrase this a bit. My master array:
var europeanCities:Array = [englandCities, franceCities];
equals to
[[London, Manchester, Leeds], [Paris, Orleans, Avignon]];
am I right? And now, how to remove the inner brackets in order to get something like this:
[London, Manchester, Leeds, Paris, Orleans, Avignon];
And please, keep in mind, that the array is much longer than englandCities and frenchCities....there are like...30 different Cities.
You can concat those together easily, and it really is the simplest option:
var englandCities:Array = ["London", "Manchester", "Leeds"];
var frenchCities:Array = ["Paris", "Orleans", "Avignon"];
var masterArray:Array = [englandCities, frenchCities];
var europeanCities:Array = new Array();
for each(var country:Array in masterArray) {
europeanCities = europeanCities.concat(country);
}
trace(europeanCities); // London,Manchester,Leeds,Paris,Orleans,Avignon
I'm not sure I understand your reason for avoiding concat for this, unless the issue is it that you don't want to duplicate the values. (So modifying englandCities[0] will also modify europeanCities[0].)
If your cities are Objects rather than primitive Strings, a concatenated Array will work fine. If they are primitives though, there's no way to do this with an Array. You could however write a function to provide similar behaviour like this:
var englandCities:Array = ["London", "Manchester", "Leeds"];
var frenchCities:Array = ["Paris", "Orleans", "Avignon"];
var allCities:Array = [englandCities, frenchCities];
function europeanCities(id:int):String {
var cityID:uint = 0;
while (id > allCities[cityID].length - 1) {
id -= allCities[cityID].length;
cityID++;
}
return allCities[cityID][id];
}
trace (europeanCities(0)); // London
trace (europeanCities(5)); // Avignon
Create an empty array, then traverse the masterArray taking any sub-arrays, and do a concat() for your new array. This will make you another array that's flat, without disturbing master array.
I just write this here because it is possible.
If you insist on not using concat here is one bad solution:
// join elements into a comma delimited string
var s: String = europeanCities.join(',');
// Split the string with delimiter as commas
europeanCities = s.split(',');
Since the subarray elements automatically will be joined with ',' regardless of join delimiter and our join delimiter is already ',' this will work.
But this solution is cpu intensive and not optimal.

Use RegExp and split to read file text flash

I have a file text more than 1 000 000 lines that begins by the character C and other one by M
Example:
C9203007870000000000000006339912610971240095400111200469300000 16122011AMI 00000100010000315 080
C9203007870000000000000006339912610971240095400111200469300000 09122011B 590001000100000270016092100
M920300787000000000000000633991261097124009540011120046930000031122011JVJF004 10 N
M920300787000000000000000633991261097124009540011120046930000009122011DEQP003 10 N
M920300787000000000000000633991261097124009540011120046930000012122011ACQK001 10Z N
C9203007870000000000000006339912610971240095400111200469300000 24122011AMI 00000100010000315 080
C9203007870000000000000006339912610971240095400111200469300000 24122011AMI 00000100010000315 080
I want to put in my array only the lines who begins with the character M
How I can add in my split: var pattern:RegExp = /^M/;
var mFileReference:FileReference;
var mArray:Array = new Array();
function onFileLoaded(event:Event):void
{
mFileReference = event.target as FileReference;
data = mFileReference["data"];
mArray = (data.toString()).split("\n");
}
I don’t want to pass by the loop ‘for’ its take a lot of time and resources
I want to add /^M/ to my split is it possible?
for each (var s:String in mArray)
{
if (pattern.test(s)) {
values.push(s);
}
}
Thanks everybody.
Try this regular expression:
/^M.*/gm
This should match all lines that begin with M and nothing else.
It uses the g flag to match all cases of the expression in the string, and it uses m for multiline mode, so ^ and $ will match the beginning/end of lines instead of the beginning/end of the string.
You can get get your array like this:
mArray = data.toString().match(/^M.*/gm);

Parsing a path in Actionscript 3?

I'm using a URLLoader to load a photo and I want to be able to display the filename of the photo based on the URLLoader's loaderInfo.url property.
Given a loader named photoLoader, what the string called fileName be?
I would take the .url property and split it into an array using the / as the delimiter. Then just grab the last item in that array to get the filename.
Code:
var pathArray:Array = photoLoader.url.split('/')
var FileName:String = pathArray[pathArray.length()-1]
with
s:String = "http:/somedomain/someurl/somefilename";
You could do
fileName = s.split('/').pop()
to return the top of the array from splitting the url at '/'
var pathArray:Array = photoLoader.url.split('/')
var FileName:String = pathArray[pathArray.length-1]
Please note that the keyword "length" is not followed by parenthesis. For arrays, it is not supposed to be a function, it is a property. On the other hand, XML lists can use the length() function.

AS3 How to make a kind of array that index things based on a object? but not being strict like dictionary

How to make a kind of array that index things based on a object? but not being strict like dictionary.
What I mean:
var a:Object = {a:3};
var b:Object = {a:3};
var dict:Dictionary = new Dictionary();
dict[a] = 'value for a';
// now I want to get the value for the last assignment
var value = dict[b];
// value doesn't exits :s
How to make something like that. TO not be to heavy as a lot of data will be flowing there.
I have an idea to use the toString() method but I would have to make custom classes.. I would like something fast..
Why not make a special class that encapsulates an array, put methods in there to add and remove elements from the array, and then you could make a special method (maybe getValueByObject(), whatever makes sense). Then you could do:
var mySpecialArrayClass:MySpecialArrayClass = MySpecialArrayClass();
var a:Object = {a:3};
var b:Object = {a:3};
mySpecialArrayClass.addElement(a,'value for a');
var value = mySpecialArrayClass.getValueByObject(a);
I could probably cook up a simple example of such a class if you don't follow.
Update:
Would something like this help?
http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/
Update:
Could you use the === functionality? if you say
if ( object === object )
it compares the underlying memory address to see if two objects are the same reference...