The following example pulls various data points from a weather site, demonstrates the use of a number of functions, and provides tips for "best use" of System/Global variables in the procecss.
An explaination of the macro follows the macro image ...
This weather info was fetched from the "weatherforyou.com" web site and saved in LOCAL1.
Next a check is made to verify that a valid result was returned. If not an error message is posted in the User Log by the code at the "ERROR" label section of the macro. Note that the label of "nextLINE" is a bogus label. It could be anything (ie, "foofoo") you want as long as it is not a valid label. If PH can't find a label with the named designation, then it just goes to the next line of code.
Thus the (bogus) "nextLINE" label is discriptive for code-flow understanding.
Assuming we have a good page returned, it turns out that this web page is rather huge, so to keep subsequent string operations from bogging down, the text in LOCAL1 is trimmed to just 5000 characters around the area where the needed data is located. An easy way to determine this "sweet area" is to use the ph_saveurl() which will save your weather site page contents to a file.
Then open the file in Windows Word (or similar program) and use the word/character count feature to locate your "sweet spot." Using that data, use the "mid" function to extract an area around your interest area, allowing for reasonable variations in subsequent web page designs.
Next we look for the "Wind" data.
It is located in an html area that looks like this ...
<span class="Label">Wind </span><span class="Value">SW 18 MPH </span>
Seq #80 then checks to see if the returned date was "Calm" rather than a wind speed in mph. Since the speed is used elsewhere and needs to be a number, it is converted from "Calm" to the number 0.
NOTE: The formula in Seq #80 was (as well as other formulas before and after) was ...
if(match (ph_getvar_s(1,2), "Calm"),"0","[LOCAL2]"),
and NOT ...
if(match ("[LOCAL2]", "Calm"),"0","[LOCAL2]")
The ph_getvar_s() is the ONLY way that you can be assured of a good string being returned, since it is possible that a complex target string may contain BOTH single (') and double (") prime characters. It is a "best practice" to always use the ph_getvar_s() function when you want to get a perfect string returned.
Seq #90 snaps the numeric value of "18" out of the string returned into LOCAL2, which was "SW 18 MPH"
Finally (Seq #100) an error check is done to assure that a numeric value is returned. If the value extracted is not a number then a default value of "3" is placed in LOCAL2.
Next the Humidity data value (Seq 3120) is extracted from the web page environment of ...
<span class="Label">Humidity </span><span class="Value">54%</span>
In Seq #130, we error check and if the Humidity value id blank, then substitute "65" as a generic value.
In Seq #140 we extract the wind chill, or "Feels Like" temp from a web page environment of ...
<span class="SmallTemp">Feels Like ° 10 F</span>
After that it is just some house keeping to round the numers to integer values.
This example gives a number if various ways to trim, find, and fetch data from complex data strings.