Forum: Ruby match a value from hash which is inside array

Posted by robert lengu (robertlengu789)
on 2013-01-20 15:27
if  result.has_key?("Value)


         p result["Value"] &&
result["#{Value}"]["query"]["location"]["data"]

end

Which will give me something like


[{"Value-1"=>"Thu Dec 20 12:41:33 EST 2012"}, {"Value-7"=>"Wed Dec 19
11:37:00 EST 2012"}, {"Value-8"=>"Sun Jan 06 12:08:58 EST 2013"}]


now from this result i want to see
if  result has  values like : Value-8 , then it will say "found the
result"

but i cant get my head round about how to do this.

mainly i think its a hash inside a array. and having issues
Can any one please help me

Thanks
Posted by jake kaiden (lljk)
on 2013-01-20 15:47
array = [{"Value-1"=>"Thu Dec 20 12:41:33 EST 2012"}, {"Value-7"=>"Wed 
Dec 19 11:37:00 EST 2012"}, {"Value-8"=>"Sun Jan 06 12:08:58 EST 2013"}]

array.each{|entry| p "found the result" if entry.include?("Value-8")}


http://www.ruby-doc.org/core-1.9.3/Array.html
Posted by Robert Klemme (robert_k78)
on 2013-01-20 17:11
(Received via mailing list)
On Sun, Jan 20, 2013 at 3:27 PM, robert lengu <lists@ruby-forum.com> 
wrote:
> if  result.has_key?("Value)
>
>
>          p result["Value"] &&
> result["#{Value}"]["query"]["location"]["data"]
>
> end
>
> Which will give me something like

No, it most likely won't.  Partly because there is a syntax error
right in the first line of the code.  You also mix "Value" and
"#{Value}" which are two totally different things.

> [{"Value-1"=>"Thu Dec 20 12:41:33 EST 2012"}, {"Value-7"=>"Wed Dec 19
> 11:37:00 EST 2012"}, {"Value-8"=>"Sun Jan 06 12:08:58 EST 2013"}]

You would need to tell us what values you have there in the Hash - if
it is a Hash at all.

> now from this result i want to see
> if  result has  values like : Value-8 , then it will say "found the
> result"

Did you mean "if result has _keys_ like 'Value-8'"?  Because you use
"Value" as key above.

> but i cant get my head round about how to do this.
>
> mainly i think its a hash inside a array. and having issues
> Can any one please help me

Frankly, I am not entirely sure what you are after.  If you want to
select based on keys in the Hash you could do:

result.select {|k,v| /\AValue-\d+\z/ =~ k}

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.