Hash and Arrays...

J

Josselin

I receive from yahoo weather an Hash like this one,

response
=> {"xmlns:geo"=>"http://www.w3.org/2003/01/geo/wgs84_pos#",
"xmlns:yweather"=>"http://xml.weather.yahoo.com/ns/rss/1.0",
"version"=>"2.0", "channel"=>[{"title"=>["Yahoo! Weather - Error"],
"description"=>["Yahoo! Weather Error"], "item"=>[{"title"=>["City not
found"], "description"=>["\n Sorry, your location 'dfghh' was not
found. Please try again.\n "]}]}]}

I need to test the response for an error on channel title, I wrote :

(response.fetch("channel")[0]).fetch("title")[0]
=> "Yahoo! Weather - Error"

to get the string, but is there any simpler way to do it ? I'm not yet
very easy with Hash...

thanks
 
B

Brian Candler

I receive from yahoo weather an Hash like this one,

response
=> {"xmlns:geo"=>"http://www.w3.org/2003/01/geo/wgs84_pos#",
"xmlns:yweather"=>"http://xml.weather.yahoo.com/ns/rss/1.0",
"version"=>"2.0", "channel"=>[{"title"=>["Yahoo! Weather - Error"],
"description"=>["Yahoo! Weather Error"], "item"=>[{"title"=>["City not
found"], "description"=>["\n Sorry, your location 'dfghh' was not
found. Please try again.\n "]}]}]}

I need to test the response for an error on channel title, I wrote :

(response.fetch("channel")[0]).fetch("title")[0]
=> "Yahoo! Weather - Error"

to get the string, but is there any simpler way to do it ?

response["channel"][0]["title"][0]

or:

response["channel"].first["title"].first

But it also depends on how you are parsing the XML. If you're using
xmlsimple then you can pass option 'ForceArray' => false, which gets rid of
the arrays. Then you can do

response["channel"]["title"]
 
J

Josselin

I receive from yahoo weather an Hash like this one,

response
=> {"xmlns:geo"=>"http://www.w3.org/2003/01/geo/wgs84_pos#",
"xmlns:yweather"=>"http://xml.weather.yahoo.com/ns/rss/1.0",
"version"=>"2.0", "channel"=>[{"title"=>["Yahoo! Weather - Error"],
"description"=>["Yahoo! Weather Error"], "item"=>[{"title"=>["City not
found"], "description"=>["\n Sorry, your location 'dfghh' was not
found. Please try again.\n "]}]}]}

I need to test the response for an error on channel title, I wrote :

(response.fetch("channel")[0]).fetch("title")[0]
=> "Yahoo! Weather - Error"

to get the string, but is there any simpler way to do it ?

response["channel"][0]["title"][0]

or:

response["channel"].first["title"].first

But it also depends on how you are parsing the XML. If you're using
xmlsimple then you can pass option 'ForceArray' => false, which gets rid of
the arrays. Then you can do

response["channel"]["title"]

thanks a lot Brian, that's exactly what I am using... so .. simple !

joss
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top