Extracting a value from a file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Extracting a value from a file
Hi, I'm have an apps (currency converter/calculator) I made that works pretty
good but after a while I see that I could make improvements. I used a test stack
for the part I want to improve with the code below. Basically this get the rate
from the web and put it in field rate and then I use it for calculation. I would
like to change the line of code "get line 480 of myfile" because once in awhile
changes are made to the web page and I have to change the line number in my
coding. I tried using FIND , SEARCH but that did not work, I think READ FROM
FILE seems a good guess but could not get it to work. The commented lines are
the one before and after line 480. Could you steer me toward the right keyword
and some basic hint how to solve this. Many thanks TIA.
byg
on mouseUp
set the numberformat to "#.000"
get url "http://www.canadianforex.ca/currency-converter/?
cid=273&gclid=COeg6IiroLQCFQqk4AodcWoAXQ"
put it into myfile
-- " = <strong>"
get line 480 of myfile
-- " <strong>"
put "$" && it + 0 into field "rate"
end mouseUp
good but after a while I see that I could make improvements. I used a test stack
for the part I want to improve with the code below. Basically this get the rate
from the web and put it in field rate and then I use it for calculation. I would
like to change the line of code "get line 480 of myfile" because once in awhile
changes are made to the web page and I have to change the line number in my
coding. I tried using FIND , SEARCH but that did not work, I think READ FROM
FILE seems a good guess but could not get it to work. The commented lines are
the one before and after line 480. Could you steer me toward the right keyword
and some basic hint how to solve this. Many thanks TIA.
byg
on mouseUp
set the numberformat to "#.000"
get url "http://www.canadianforex.ca/currency-converter/?
cid=273&gclid=COeg6IiroLQCFQqk4AodcWoAXQ"
put it into myfile
-- " = <strong>"
get line 480 of myfile
-- " <strong>"
put "$" && it + 0 into field "rate"
end mouseUp
Re: Extracting a value from a file
Hi byg,
Gosh so many fun ways to do this!
or
But I have to admit those really are not very good, they are not robust enough.
You probably would be better off using an rss feed like:
http://www.bankofcanada.ca/stats/assets ... en_USD.xml (just type "exchange rss" into google to get others)
there you have:
<cb:value decimals="4">0.9708</cb:value>
<cb:value decimals="4"> and </cb:value> are unique to that page.
Don't worry you don't even have to use any of liveCodes xml stuff to get the info.
Simon
Gosh so many fun ways to do this!

Code: Select all
put lineOffset("= <strong>", myfile) into tLine
put line (tLine + 1) of myfile into tPrice
Code: Select all
on mouseUp
put url "http://www.canadianforex.ca/currency-converter/?" into tHtml
repeat for each line tLine in tHtml
if char 1 of tLine is a number then put tLine & cr after tPrice
end repeat
breakpoint
end mouseUp
You probably would be better off using an rss feed like:
http://www.bankofcanada.ca/stats/assets ... en_USD.xml (just type "exchange rss" into google to get others)
there you have:
<cb:value decimals="4">0.9708</cb:value>
<cb:value decimals="4"> and </cb:value> are unique to that page.
Don't worry you don't even have to use any of liveCodes xml stuff to get the info.
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Extracting a value from a file
Oh, I just can't resist 
Not really teaching you "how to fish" 
Simon

Code: Select all
on mouseUp
put url "http://www.bankofcanada.ca/stats/assets/rates_rss/closing/en_USD.xml" into tXml
filter tXml with "*<cb:value*"
put offset(">",tXml) into tStart
put offset("<",tXml,tStart) into tEnd
put char (tStart +1) to (tEnd + tStart) -1 of tXml into tPrice
breakpoint
end mouseUp

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Extracting a value from a file
Thanks Simon, that works great, you are right now I have to learn how to fish or understand why this works. Thanks again.
byg
byg
Re: Extracting a value from a file
Yeah I should comment more:
Hope that is better.
Simon
Code: Select all
on mouseUp
put url "http://www.bankofcanada.ca/stats/assets/rates_rss/closing/en_USD.xml" into tXml --put an rss feed into tXml
breakpoint
filter tXml with "*<cb:value*" --there is only 1 line I'm interested in (look in tXml), get rid of the rest. Note the wildcards "*"
put offset(">",tXml) into tStart --in what remains of tXml I only want the price so tell me the number of characters to the first ">"
put offset("<",tXml,tStart) into tEnd -- after the numbers is a "<". Watch out offset(x,x,number of characters to skip) doesn't return what I thought it would.
put char (tStart +1) to (tEnd + tStart) -1 of tXml into tPrice --using tStart and tEnd I now can find just the numbers. Note tEnd + tStart... as mentioned above
--if you remove the +1 or -1 above you can see what you would get just from offset
breakpoint
end mouseUp
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Extracting a value from a file
Very good explanation I will be going thru it again, thanks
byg
byg