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(":");
I am experimenting with a Firefox extension that will load an arbitrary URL (only via HTTP or HTTPS) when certain conditions are met.
With certain conditions, I just want to display a message instead of requesting a URL from the internet.
I was thinking about simply hosting a local webpage that would display the message. The catch is that the message needs to include a variable.
Is there a simple way to craft a local web page so that it can display a variable passed to it in the URL? I would prefer to just use HTML and CSS, but adding a little inline javascript would be okay if absolutely needed.
As a simple example, when the extension calls something like:
folder/messageoutput.html?t=Text%20to%20display
I would like to see:
Message: Text to display
shown in the browser's viewport.
You can use the "search" property of the Location object to extract the variables from the end of your URL:
var a = window.location.search;
In your example, a will equal "?t=Text%20to%20display".
Next, you will want to strip the leading question mark from the beginning of the string. The if statement is just in case the browser doesn't include it in the search property:
var s = a.substr(0, 1);
if(s == "?"){s = substr(1);}
Just in case you get a URL with more than one variable, you may want to split the query string at ampersands to produce an array of name-value pair strings:
var R = s.split("&");
Next, split the name-value pair strings at the equal sign to separate the name from the value. Store the name as the key to an array, and the value as the array value corresponding to the key:
var L = R.length;
var NVP = new Array();
var temp = new Array();
for(var i = 0; i < L; i++){
temp = R[i].split("=");
NVP[temp[0]] = temp[1];
}
Almost done. Get the value with the name "t":
var t = NVP['t'];
Last, insert the variable text into the document. A simple example (that will need to be tweaked to match your document structure) is:
var containingDiv = document.getElementById("divToShowMessage");
var tn = document.createTextNode(t);
containingDiv.appendChild(tn);
getArg('t');
function getArg(param) {
var vars = {};
window.location.href.replace( location.hash, '' ).replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);
if ( param ) {
return vars[param] ? vars[param] : null;
}
return vars;
}
This might be obvious to some people, but I have CSV data that I'm storing as a String in which every number is off by -1. I'd like to write a function (in ActionScript 3) in which I go in and increase every value by +1. How can I do this?
My CSV String looks like this:
public static const CSV_DATA:String = "14,15,16,8,9,8,9,8,9,8,9,264,265,266,267,268,269,8,9,260,261,262,263,8,9,1,2,3\n" +
"32,33,34,26,27,26,27,26,27,26,27,282,283,284,285,286,287,26,27,278,279,280,281,26,27,19,20,21\n" +
... etc
Thank you in advance.
In your case, just use String.split(). Primary split string by '\n', secondary split by ','. In result array every string with number process with parseInt function. After that, you can increase your numbers by one.
But if you need to read real CSV files, you can write class for this or use open-source
Here you go:
var CSV_DATA:String = "14,15,16,8,9,8,9,8,9,8,9,264,265,266,267,268,269,8,9,260,261,262,263,8,9,1,2,3,32,33,34,26,27,26,27,26,27,26,27,282,283,284,285,286,287,26,27,278,279,280,281,26,27,19,20,21";
var UPDATED_CSV_DATA:String = addOne(CSV_DATA);
function addOne(CSV:String):String
{
var CSVArr:Array = CSV.split(",");
for(var i=0;i<CSVArr.length;i++)
{
CSVArr[i] = Number(CSVArr[i]) + 1 ;
}
return CSVArr.toString();
}
I am currently trying to request a token from Flickr to then be able to do some calls to their OAuth methods. I know I must be doing something wrong, for I get a reply that the signature is wrong, but honestly I followed their instructions (http://www.flickr.com/services/api/auth.oauth.html, http://www.flickr.com/services/api/auth.oauth.html#request_token, http://www.flickr.com/services/api/flickr.auth.oauth.getAccessToken.html) but I still get an error:
oauth_problem=signature_invalid&debug_sbs=GET&http%3A%2F%2Fwww.flickr.com%2Fservices%2Foauth%2Frequest_token&oauth_callback%3D%26oauth_consumer_key%3Da0f20d2c9b0a142848cffdf9d9a5ad78%26oauth_nonce%3DFCBB713F-581E-4BC6-42FF-C50252D839EC%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1330450158%26oauth_version%3D1.0
I don't get how to create that signature nor how to put it in the request, could anybody point me in the right direction? Thanks!
I am currently working with AS3, below is my code:
// request params
var now:Date = new Date();
var requestParams:Object = {};
requestParams.oauth_callback = ""; // there is no callback, it's a desktop application
requestParams.oauth_consumer_key = API_KEY;
requestParams.oauth_nonce = UIDUtil.getUID(now);
requestParams.oauth_timestamp = String(now.time).substring(0, 10);
requestParams.oauth_signature_method = "HMAC-SHA1";
requestParams.oauth_version = "1.0";
// create an array to sort param names alphabetically
// mandatory to create signature
var sortedRequestParamNames:Array = [];
var name:String;
for(name in requestParams)
{
sortedRequestParamNames.push(name);
}
sortedRequestParamNames.sort();
// create signature
// see http://www.flickr.com/services/api/auth.spec.html#signing
var oauthSignature:String = API_SECRET;
var i:uint;
var numParams:uint = sortedRequestParamNames.length;
var paramName:String;
for(i = 0; i < numParams; i++)
{
paramName = sortedRequestParamNames[i];
oauthSignature += paramName + convertToPercentEntities(requestParams[paramName]);
}
oauthSignature = MD5.hash(oauthSignature);
// build request
var tokenRequestString:String = REQUEST_TOKEN_URL;
for(i = 0; i < numParams; i++)
{
paramName = sortedRequestParamNames[i];
tokenRequestString += (i == 0) ? "?" : "&";
tokenRequestString += paramName + "=" + requestParams[paramName];
}
tokenRequestString += "&oauth_signature=" + oauthSignature;
var tokenRequest:URLRequest = new URLRequest(tokenRequestString);
tokenRequest.method = URLRequestMethod.GET;
// load request
initLoader();
_loader.addEventListener(Event.COMPLETE, requestTokenLoadedHandler);
_loader.load(tokenRequest);
Basically, you need to use the HMAC-SHA1 algorithm instead of an MD5. I'll walk you through it.
1. Create the signature base string
You seem to be doing that (but you are assigning it directly to the signature variable). Compiling the base string is done by concatenating three different parts.
Convert the HTTP Method to uppercase and set the base string equal to this value. Example: GET
Append the '&' character to the base string.
Percent encode the URL (without parameters) and append it to the base string. Example: http%3A%2F%2Fexample.com%2Frequest
Append the '&' character to the base string.
Percent encode the sorted parameter string and append it to the base string.
It should end up looking like this:
GET&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q
%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_
key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_m
ethod%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk
9d7dh3k39sj
Now you're done with the signature base string. Lets move on to
2. Figuring out your signing key.
Your signing key is on this format: CONSUMER_SECRET + "&" + TOKEN_SECRET. But since you do not have a token yet, the signing key is the consumer secret and an ampersand. Like this: CONSUMER_SECRET + "&".
For all requests, except the first one your will have a token though, either a request token or an access token.
3. Combine the key and the base string using the HMAC-SHA1 algorithm.
I have used http://code.google.com/p/as3crypto/ when signing with AS3. You can even test its HMAC-SHA1 algorithm on this demo page: http://crypto.hurlant.com/demo/.
Use the base string as input, and the signing key as key to the HMAC-SHA1 algorithm.
The output of the HMAC-SHA1 algorithme will be a binary string which needs to be base64 encoded to produce the final signature. It should look something like this:
NYIQGEwIomgCuVOIA28pMDMID78=
This should be send with the request as the oauth_signature parameter.
I had problem with it too, ended up using FlickrNet API.
http://flickrnet.codeplex.com/
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.