Separate a string in 2 variables - actionscript-3

I've got a string with 2 weblinks.
I've put an "#" symbol to separate them.
var yourString:String = http://www.weblink.nc/sons/silence.mp3#http://www.weblink.nc/sons/test.png;
Now, how can I separate those 2 weblink into 2 different variable (web1 and web 2)?
(something with "yourString.split(#)") ?
for this result :
var web1 = http://www.weblink.nc/sons/silence.mp3
var web2 = http://www.weblink.nc/sons/test.png;
Thank you !

Try something like this:
var yourString :String = "http://www.weblink.nc/sons/silence.mp3#http://www.weblink.nc/sons/test.png";
var stringParts :Array = new Array();
stringParts = yourString.split("#");
var web1 :String = stringParts[0];
var web2 :String = stringParts[1];
trace( "checking String web1 : " + web1 );
trace( "checking String web2 : " + web2 );
//trace( "checking Array slot A : " + stringParts[0] );
//trace( "checking Array slot B : " + stringParts[1] );
Which gives trace result of:
checking String web1 : http://www.weblink.nc/sons/silence.mp3
checking String web2 : http://www.weblink.nc/sons/test.png

Related

How to take a specific info from a string in json format?

I've got this AS3 code :
var myString:String;
var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274");
var loader:URLLoader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);
function weatherLoaded(e:Event):void{
myString = e.target.data;
trace(myString); //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27°C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png' title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"°"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}}
var myData : Object = JSON.parse(e.target.data);
for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); }
trace(myData); }
My String is containing lots of infos.
How could I take specifics informations ?
Exemple:
If I want to take the swell (in this example, the swell is : "0.4 m # 13 s"). How could I do that? (the purpose is to displays it in a text box like that :
function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata =.... ?
info_txt.text = swell_AnseVata;
}
Thx
Just set a breakpoint after you parse the data and examine the myData in the debugger - you will see the object structure. Or just trace the whole object structure out:
import mx.utils.ObjectUtil;
trace(ObjectUtil.toString(myData));
In your case you'd need to put your string together out of the vars in your object:
var hs_sw:Object = myData.vars.hs_sw;
var tp:Object = myData.vars.tp;
trace(hs_sw.value + " " + hs_sw.unit + " # " + tp.value + " " + tp.unit);

Make my AS3 code go fetch information on a website that I don't own

There is this website : http://www.swellmap.co.nz/ and I'd like to make my AS3 code go fetch some infos and displays it flash.
Is it possible if I don't own the website ?
Exemple :
I want to display these infos in my AS3 code. Is this possible ? How can I do ?
Thx for your help,
EDIT
Thx to the full answer of VC.One I've managed to paste infos in a String.
Here's what I did :
var myString:String;
var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274");
var loader:URLLoader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);
function weatherLoaded(e:Event):void{
myString = e.target.data;
trace(myString); //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27°C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png' title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"°"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}}
}
Now, I didn't quite understand how could I retrieve only some infos (like the swell for exemple).
Is it possible to show me in AS3 code, how could I do that ? (in this exemple, we can see that the swell is "0.4 m # 13 s")
exemple of what I'd like to do :
function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata =.... ?
info_txt.text = swell_AnseVata;
}
If you use the Developer Tools of your browser then you'll see that there's a PHP file being accessed to get the information. It starts http://www.swellmap.co.nz/ajr.php?r= and you need to find what it says for you. Now to view the contents just remove the part of the URL with &callback=XYZ where XYZ will be whatever the link has..
1)
Here's an example of how to get data for a location :
http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=ZZZZZ&country=nz&swellmap=1&country=YYY&swellmap=1&_=1460950764514
Replace &s=ZZZZZ with name of location, so if I want Anse Vata it becomes &s=Anse%20Vata and La Nera becomes &s=La%20Nera. So use %20 for any spaces in location name. Replace &country=YYY with example &country=fra.
2)
To get the JSON data for Anse Vata, declare your new String variable to later hold the JSON text and then just use URLStream in AS3 to load the link (open in browser tab to check contents) : http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274
In the Event Complete listener function of URLStream you just use (example) myString = e.target.data;. Then use a JSON parser on your myString or do it manually yourself using String functions (like indexOf to word search).
3) If you opened the above link in an new tab you'll see the JSON entries you need to parse.
Swell : "hs_sw":{"value":"0.4","title":"Swell","unit":"m"} and for
extracting the # 13 s period use :
"tp":{"value":"13","title":"Period","unit":"s"}
Wind : "windma":{"value":"E 12","title":"Wind","unit":"kts"}
Icon : "summary":"<img class='wx-summary'
src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png'
You'll have to clean up the icon links so it becomes for example :
http://www.swellmap.co.nz/style/img/weathericons/suncloud.png
EDIT :
The code below extracts the expected information from the JSON string. Just replace &s= with any other location (eg: &s=Ilot%20Tenia) and it extracts the Swell, Wind and Icon URL entries...
var myURL : String = "http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274";
var URL_Req : URLRequest = new URLRequest( myURL );
var URL_load:URLLoader = new URLLoader();
URL_load.addEventListener(Event.COMPLETE, completeHandler);
try { URL_load.load( URL_Req ); }
catch (error:Error)
{ trace("Could not load - Please check URL is correct : " + error.message); }
var idx_start : int = 0; var idx_end : int = 0;
var str_Swell : String = "";
var str_Swell_Period : String = "";
var str_Swell_Dir : String = "";
var str_Wind : String = ""; var url_Icon : String = "";
function completeHandler(e : Event):void
{
//var myData : Object = JSON.parse(e.target.data);
//for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); }
var myString : String = String(e.target.data);
//trace ( "myString : " + myString);
//# Get Swell
idx_start = myString.indexOf("\"hs_sw\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Swell = myString.substring(idx_start + 18, idx_end-1);
str_Swell = str_Swell + " m";
//trace ("Swell : " + str_Swell );
//# Get Direction (for Swell)
idx_start = myString.indexOf("\"dpm\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Swell_Dir = myString.substring(idx_start + 16, idx_end-1);
//trace ("Swell dir : " + str_Swell_Dir );
//# Get time Period (for Swell)
idx_start = myString.indexOf("\"tp\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Swell_Period = myString.substring(idx_start + 15, idx_end-1);
str_Swell_Period = " # " + str_Swell_Period + " s";
//trace ("Period : " + string_Period );
//# Join Swell Direction, Size & Period entries into one sentence
str_Swell = str_Swell_Dir + " " + str_Swell + str_Swell_Period;
trace ("Swell : " + str_Swell );
//# Get Wind
idx_start = myString.indexOf("\"windma\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Wind = myString.substring(idx_start + 19, idx_end-1);
str_Wind = str_Wind + " kts";
trace ("Wind : " + str_Wind );
//# get Image URL
idx_start = myString.indexOf("\'wx-summary\'" , 0 );
idx_end = myString.indexOf("'" , idx_start + 18 );
url_Icon = myString.substring(idx_start + 18, idx_end);
url_Icon = url_Icon.replace(/\\/g, "");
trace ("URL : " + url_Icon );
//# load image using : new URLRequest (url_Icon);
}

How to create a projection widget programmatically in orchard cms?

I have created a query in my module with this code:
var myName = "something"
var theQuery = _contentManager.Create("Query");
theQuery.As<TitlePart>().Title = myName + "Query";
var filterGroupRecord = new FilterGroupRecord();
var filterRecord = new FilterRecord()
{
Category = "Content", Type = "ContentTypes",
Description = myName , Position = 1,
State = "<Form><Description>" + myName
+ "</Description> <ContentTypes>" + myName + "</ContentTypes></Form>"
};
filterGroupRecord.Filters.Insert(0, filterRecord);
theQuery.As<QueryPart>().FilterGroups.Clear();
theQuery.As<QueryPart>().FilterGroups.Insert(0, filterGroupRecord);
And I know to create a projection widget I should use below code :
var theProjectionWidget = _contentManager.Create("ProjectionWidget");
theProjectionWidget .As<WidgetPart>().Title = myName + "ProjectionWidget";
theProjectionWidget .As<WidgetPart>().RenderTitle = false;
theProjectionWidget .As<WidgetPart>().Zone = "Content";
theProjectionWidget .As<WidgetPart>().Position = "1";
theProjectionWidget .As<WidgetPart>().LayerPart.Name = myName;
But I don't know how to assign the above query to this new projection widget.
How to assign query id to ProjectionPart.QueryLayoutRecordId???!!
I would appreciate any help.
These are links to this discussion in codeproject & codeplex:
http://www.codeproject.com/Questions/876128/How-to-create-a-projection-widget-programmatically
https://orchard.codeplex.com/discussions/580735
After some wrestling with code and debug I found solution for my question.
Anybody with my problem can use this code:
theProjectionWidget.As<ProjectionPart>().Record.QueryPartRecord = new QueryPartRecord(){
ContentItemRecord = theQuery.As<QueryPart>().ContentItem.Record,
FilterGroups = theQuery.As<QueryPart>().FilterGroups,
Id = theQuery.As<QueryPart>().Id,
Layouts = theQuery.As<QueryPart>().Layouts,
SortCriteria = theQuery.As<QueryPart>().SortCriteria
};

Pass actual variable object to a function

The problem is simple:
I need to pass actual variable to a function.
private var test:String = "KKK";
trace (" Before --->>> " + test);
testFunction(test);
trace (" Next --->>> " + test);
private function testFunction(d:String):void{
d = "MMM";
}
Result:
Before --->>> KKK
Next --->>> KKK
The result is correct but, What I want is, send the actual test variable to my function and change that. So I want to have output like this:
Before --->>> KKK
Next --->>> MMM
Any solution?
Thanks for your answer but if I have a code like this, I need to pass the actual variable to my function:
if ( lastPos == -1 ){// if this is first item
flagLEFT = "mid";
tempImageLEFT = new Bitmap(Bitmap(dataBANK[0]["lineimage" + 10]).bitmapData);
}else if (nextPos == -1){// if this is the last position
flagRIGHT = "mid";
tempImageRGHT = new Bitmap(Bitmap(dataBANK[0]["lineimage" + 13]).bitmapData);
}
As you see, changes are in flagLEFT and tempImageRGHT . Also I have a change on numbers (10 and 13) which can be handle in normal way. I need something like this:
private function itemFirstLast(flag:String, bmp:Bitmap, pos:int):void{
flag = "mid";
bmp = new Bitmap(Bitmap(dataBANK[0]["lineimage" + pos]).bitmapData);
}
Any solution?
One way is to return the new string and assign it to test :
private var test:String = "KKK";
trace (" Before --->>> " + test);
test = testFunction(test);
trace (" Next --->>> " + test);
private function testFunction(d:String):String{
d = "MMM";
return d;
}
This still doesn't pass the actual string object but the test string will change. Strings are passed by value in AS3, if you wan't to actually pass it in you can wrap it in an object :
var object:Object {
"test":"KKK"
};
trace (" Before --->>> " + object["test"]);
testFunction(object);
trace (" Next --->>> " + object["test"]);
private function testFunction(o:Object):void{
o["test"] = "MMM";
}
You'll need to wrap it in a class instance:
class StringValue{
function StringValue( value : String ) : void{
this.value = value;
}
public var value : String;
public function toString() : String{
return value;
}
}
private var test:StringValue = new StringValue( "KKK" );
trace (" Before --->>> " + test);//traces 'KKK'
testFunction(test);
trace (" Next --->>> " + test);//traces 'MMM'
private function testFunction(d:StringValue):void{
d.value = "MMM";
}
Please refer to the following link:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f56.html
You need to wrap it in the Object .

Getting swf boolean parameters

I have a swf that I call as
/SWFUploader/upload.swf?single=true
Then in ActionScript 3 I read in the value. But it's not working. Here's my test code (the first block is taken from http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html ):
var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]);
trace(keyStr + " = " + valueStr);
}
var isSingle:Boolean = this.loaderInfo.parameters.single as Boolean;
var isSingle1:Boolean = this.loaderInfo.parameters['single'] as Boolean;
var isSingle2:Boolean = LoaderInfo(this.root.loaderInfo).parameters['single'] as Boolean;
var isSingle3:Boolean = LoaderInfo(this.root.loaderInfo).parameters.single as Boolean;
trace(isSingle + ", " + isSingle1 + ", " + isSingle2 + ", " + isSingle3);
And frustratingly this is the resulting two lines that are traced:
single = true
false, false, false, false
What am I doing wrong?
In ActionScript not empty and not null String is casted to Boolean true.
var singleStr:String = this.loaderInfo.parameters.single;
var singleBool:Boolean = singleStr == "true";