Convert string timer to integer seconds - string

i'm trying to convert an string like "0:13:30", which consist of h:mm:ss, to an integer which will be an answer of (m*60)+(s), working only with the minutes and seconds in greasemonkey or jscript.
What i curently have is:
var t_str = ''; var t_int =0;
var str1='';var str2='';var t_int1=0;var t_int2=0;
t_str="0:13:30";
alert(t_str);
str1=t_str[4]+t_str[5];
str2=t_str[2]+t_str[3];
t_int1=parseInt (str1);
t_int2=parseInt (str2);
t_int2=t_int2 * 60;
t_int=t_int1+t_int2;
alert(t_int);
I get up to the first alert. how do i get it to assign the values "13" and "30" to str2 and str1? Sorry for a basic question, but im not used to this language :)

var time = t_str.split(":"),
h = 3600 * parseInt(time[0], 10),
m = 60 * parseInt(time[1], 10),
s = parseInt(time[2], 10);
alert(h+m+s);

I assume you are using javascript. If yes, then you can use split() method to split the t_str first and then carry on with your parsing of integers.
`str = t_str.split(":");` //- array of {0,13,30}
and then use this array to access your numbers.

Related

Converting geo coordinates Format

i am asked to show a gps device on a google map. i have written a listener and the format looks something like this
1724.1543,N,07822.4276,E
how to convert these into degrees, minutes and seconds format like
17.241543,78.224276
Apparently google maps recognizes lat lng in the above format only.
My listener is written in nodejs, so a javascript way to convert is more appreciated.
thanks.
javascript way to convert lat and long to degree,minute and second is as follows:
//to convert pass lat or long to this function
function DECtoDMS(dd)
{
var vars = dd.split('.');
var deg = vars[0];
var tempma = "0."+vars[1];
tempma = tempma * 3600;
var min = Math.floor(tempma / 60);
var sec = tempma - (min * 60);
return deg+"-"+min+"-"+sec;
}
//call the function
var inDeg = DECtoDMS(72.8479400);
console.log(inDeg);

Save images from adresses with an ID and a random string part using iMacro

I want to save some images from a website, with addresses like this
http://somewebsite.com/{{ID}}_{{rand}}.JPG
{{ID}} varies from 01 to 99. The {{rand}} part is a string of 8 random uppercase characters for each ID. For example:
http://somewebsite.com/93_ABCDEFGHI.JPG
Is it possible to check all cases for the string part? if yes, could you please tell me how?
If you still want to get a random string from 208+ milliards:
SET frc "function frc() {return String.fromCharCode(65 + Math.floor(Math.random()*26))};"
SET rand EVAL("eval('{{frc}}'); var s = ''; for (i = 1; i <= 8; i++) s += frc(); s;")
http://somewebsite.com/{{ID}}_{{rand}}.JPG
(They are practically unrepeatable.)

Grabbing text from webpage and storing as variable

On the webpage
http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463
It lists prices for a particular item in a game, I wanted to grab the "Current guide price:" of said item, and store it as a variable so I could output it in a google spreadsheet. I only want the number, currently it is "643.8k", but I am not sure how to grab specific text like that.
Since the number is in "k" form, that means I can't graph it, It would have to be something like 643,800 to make it graphable. I have a formula for it, and my second question would be to know if it's possible to use a formula on the number pulled, then store that as the final output?
-EDIT-
This is what I have so far and it's not working not sure why.
function pullRuneScape() {
var page = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var number = page.match(/Current guide price:<\/th>\n(\d*)/)[1];
SpreadsheetApp.getActive().getSheetByName('RuneScape').appendRow([new Date(), number]);
}
Your regex is wrong. I tested this one successfully:
var number = page.match(/Current guide price:<\/th>\s*<td>([^<]*)<\/td>/m)[1];
What it does:
Current guide price:<\/th> find Current guide price: and closing td tag
\s*<td> allow whitespace between tags, find opening td tag
([^<]*) build a group and match everything except this char <
<\/td> match the closing td tag
/m match multiline
Use UrlFetch to get the page [1]. That'll return an HTTPResponse that you can read with GetBlob [2]. Once you have the text you can use regular expressions. In this case just search for 'Current guide price:' and then read the next row. As to remove the 'k' you can just replace with reg ex like this:
'123k'.replace(/k/g,'')
Will return just '123'.
https://developers.google.com/apps-script/reference/url-fetch/
https://developers.google.com/apps-script/reference/url-fetch/http-response
Obviously, you are not getting anything because the regexp is wrong. I'm no regexp expert but I was able to extract the number using basic string manipulation
var page = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var TD = "<td>";
var start = page.indexOf('Current guide price');
start = page.indexOf(TD, start);
var end = page.indexOf('</td>',start);
var number = page.substring (start + TD.length , end);
Logger.log(number);
Then, I wrote a function to convert k,m etc. to the corresponding multiplying factors.
function getMultiplyingFactor(symbol){
switch(symbol){
case 'k':
case 'K':
return 1000;
case 'm':
case 'M':
return 1000 * 1000;
case 'g':
case 'G':
return 1000 * 1000 * 1000;
default:
return 1;
}
}
Finally, tie the two together
function pullRuneScape() {
var page = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var TD = "<td>";
var start = page.indexOf('Current guide price');
start = page.indexOf(TD, start);
var end = page.indexOf('</td>',start);
var number = page.substring (start + TD.length , end);
Logger.log(number);
var numericPart = number.substring(0, number.length -1);
var multiplierSymbol = number.substring(number.length -1 , number.length);
var multiplier = getMultiplyingFactor(multiplierSymbol);
var fullNumber = multiplier == 1 ? number : numericPart * multiplier;
Logger.log(fullNumber);
}
Certainly, not the optimal way of doing things but it works.
Basically I parse the html page as you did (with corrected regex) and split the string into number part and multiplicator (k = 1000). Finally I return the extracted number. This function can be used in Google Docs.
function pullRuneScape() {
var pageContent = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var matched = pageContent.match(/Current guide price:<.th>\n<td>(\d+\.*\d*)([k]{0,1})/);
var numberAsString = matched[1];
var multiplier = "";
if (matched.length == 3) {
multiplier = matched[2];
}
number = convertNumber(numberAsString, multiplier);
return number;
}
function convertNumber(numberAsString, multiplier) {
var number = Number(numberAsString);
if (multiplier == 'k') {
number *= 1000;
}
return number;
}

Actionscript Convert String to Int

I am using Actionscript 2.0
In a Brand new Scene. My only bit of code is:
trace(int('04755'));
trace(int('04812'));
Results in:
2541
4812
Any idea what I am doing wrong/silly?
By the way, I am getting this source number from XML, where it already has the leading 0. Also, this works perfect in Actionscript 3.
In AS3, you can try:
parseInt('04755', 10)
10 above is the radix.
parseInt(yourString);
...is the correct answer. .parseInt() is a top-level function.
Converting a string with a leading 0 to a Number in ActionScript 2 assumes that the number you want is octal. Give this function I've made for you a try:
var val:String = '00010';
function parse(str:String):Number
{
for(var i = 0; i < str.length; i++)
{
var c:String = str.charAt(i);
if(c != "0") break;
}
return Number(str.substr(i));
}
trace(parse(val)); // 10
trace(parse(val) + 10); // 20
Basically what you want to do now is just wrap your string in the above parse() function, instead of int() or Number() as you would typically.
Bit of a simple one...
try this -
temp="120";
temp2="140";
temp3=int ( temp );
temp4=int ( temp2 );
temp5=temp4+temp3;
trace(temp5);
so, all you need is...
int("190");

How to find position of nth token

We have a string that has a maximum limit of 20 words. If the user enters something that is more than 20 words, we then need to truncate the string at its 20th word. How can we automate this? We are able to find the 20th token with #GetToken(myString, 20, ' ')#, but are unsure on how to find it's position in order to left-trim. Any ideas?
Thanks in advance.
The UDF ListLeft() should do what you want. It takes a list and returns the list with the number of elements you define. "Space" is fine as a delimiter.
/**
* A Left() function for lists. Returns the n leftmost elements from the specified list.
*
* #param list List you want to return the n leftmost elements from.
* #param numElements Number of leftmost elements you want returned.
* #param delimiter Delimiter for the list. Default is the comma.
* #return Returns a string,
* #author Rob Brooks-Bilson (rbils#amkor.com)
* #version 1, April 24, 2002
*/
function ListLeft(list, numElements){
var tempList="";
var i=0;
var delimiter=",";
if (ArrayLen(arguments) gt 2){
delimiter = arguments[3];
}
if (numElements gte ListLen(list, delimiter)){
return list;
}
for (i=1; i LTE numElements; i=i+1){
tempList=ListAppend(tempList, ListGetAt(list, i, delimiter), delimiter);
}
return tempList;
}
p.s. CFLIB.org is an outstanding resource, and is usually my first stop when I'm looking for something like this. I recommend it highly.
Can also use a regular expression (group #1 contains match): ^(?:\w+\s+){19}(\w+)
Maybe you could avoid trimming and instead rebuild the result from scratch, something like (pseudo-code, I don't know ColdFusion):
result = ''
for (i = 0; i < 20; ++i)
{
result = result + GetToken(myString, i, ' ');
}
Would that work?
Not sure if CF provides this, but generally there is a LastIndexOf(string token) method. Use that combined with a substring function. For isntance (psuedocode):
string lastWord = GetToken(myString, 20, ' ');
string output = Substring(mystring, 0, LastIndexOf(mystring, lastWord)+StrLength(lastWord));

Resources