[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash and Arrays...

Josselin

4/24/2007 10:42:00 AM

I receive from yahoo weather an Hash like this one,

response
=> {"xmlns:geo"=>"http://www.w3.org/2003/01/geo/wgs84_...,
"xmlns:yweather"=>"http://xml.weather.yahoo.com/ns/rss...,
"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 Answers

Brian Candler

4/24/2007 11:18:00 AM

0

On Tue, Apr 24, 2007 at 07:45:04PM +0900, Josselin wrote:
> I receive from yahoo weather an Hash like this one,
>
> response
> => {"xmlns:geo"=>"http://www.w3.org/2003/01/geo/wgs84_...,
> "xmlns:yweather"=>"http://xml.weather.yahoo.com/ns/rss...,
> "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"]

Josselin

4/25/2007 7:47:00 AM

0

On 2007-04-24 13:17:37 +0200, Brian Candler <B.Candler@pobox.com> said:

> On Tue, Apr 24, 2007 at 07:45:04PM +0900, Josselin wrote:
>> I receive from yahoo weather an Hash like this one,
>>
>> response
>> => {"xmlns:geo"=>"http://www.w3.org/2003/01/geo/wgs84_...,
>> "xmlns:yweather"=>"http://xml.weather.yahoo.com/ns/rss...,
>> "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