Hash and Arrays


#1

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


#2

On Tue, Apr 24, 2007 at 07:45:04PM +0900, Josselin wrote:

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”]


#3

On 2007-04-24 13:17:37 +0200, Brian C. removed_email_address@domain.invalid said:

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