views:

231

answers:

1

I'm fairly new to lua programming, but I'm also a quick study. I've been working on a weather forecaster for a program that I use, and it's working well, for the most part. Here is what I have so far. (Pay no attention to the zs.stuff. That is program specific and has no bearing on the lua coding.)

if not http then http = require("socket.http") end  

local locale = string.gsub(zs.params(1),"%s+","%%20")
local page = http.request("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" .. locale .. "&wuSelect=WEATHER")
local location = string.match(page,'title="([%w%s,]+) RSS"')
--print("Gathering weather information for " .. location .. ".")
--local windspeed = string.match(page,'<span class="nobr"><span class="b">([%d.]+)</span>&nbsp;mph</span>')
--print(windspeed)
local condition = string.match(page, '<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />')
--local image = string.match(page, '<img src="http://icons-ecast.wxug.com/i/c/a/(.+).gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />')
local temperature = string.match(page,'pwsvariable="tempf" english="&deg;F" metric="&deg;C" value="([%d.]+)">')
local humidity = string.match(page,'pwsvariable="humidity" english="" metric="" value="(%d+)"')
zs.say(location)
--zs.say("image ./Images/" .. image .. ".gif")
zs.say("<color limegreen>Condition:</color> <color white>" .. condition .. "</color>")
zs.say("<color limegreen>Temperature: </color><color white>" .. temperature .. "F</color>")
zs.say("<color limegreen>Humidity: </color><color white>" .. humidity .. "%</color>")

My main issue is this: I changed the 'condition' and added the 'image' variables to what they are now. Even though the line it's supposed to be matching comes directly from the webpage, it fails to match at all. So I'm wondering what it is I'm missing that's preventing this code from working. If I take out the
<td class="vaM taC">< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif"
it'll match condition flawlessly. (For whatever reason, I can't get the above line to display correctly, but there is no space between the `< and img)

Can anyone point out what is wrong with it? Aside from the pattern matching, I assure you the line is verbatim from the webpage.

Another question I had is the ability to match across line breaks. Is there any possible way to do this? The reason why I ask is because on that same page, a few of the things I need to match are broken up on separate lines, and since the actual pattern I'm wanting to match shows up in other places on the page, I need to be able to match across line breaks to get the exact pattern. I appreciate any help in this matter!

+1  A: 

You can simplify your match considerably (see below) but in general it looks like you've got two problems...

  • Missing the () around the match you wish to capture.
  • You need to escape the . characters in your match by making them %.

I tried this and it worked...

local page = [[<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/hello_world.gif" width="42" height="42" alt="HELLO WOLRD" class="condIcon" />]]
local condition, image = string.match(page, '.+/([%w_]+)%.gif".+alt="([%w%s]+)".+')
print(condition, image)

this printed...

hello_world    HELLO WORLD

as for multiline, that should not be a problem, the newlines are just control characters and if you read in multiple lines into the same string this match works.

Fraser Graham
Excellent! It always helps to have a fresh set of eyes. Thanks! However, I came across another issue, and I can't seem to figure this out one, either. if location then do the zs.say stuff here else print("Location not found!") end I can't seem to get that to work. What I want it to do is if the location cannot be found to notify me of my error. However, no matter how I do that, it seems to bypass it and ignores the if statement completely. I'm assuming it's another thing I'm missing, though. Again, appreciate the help!
Josh
Try printing the value of location to verify it is what expect before your if statement, if location is an empty string then "if location" will evaluate to true
Fraser Graham
Thanks! I actually found out it was an error on my part - I had the if location portion in the wrong section. I should have put it before print("Gathering informantion ...") where I concatenated the location. I /think/ I can get the rest from here now. Thanks for all your help!
Josh