From: "Damián M. González" Date: 2012-12-09T20:55:07+09:00 Subject: Re: Searching a hash by value Ferdous ara wrote in post #1088371: > Hi > I am looking at google for this solution > i got few commands to do that... but i cant figured it out , > > example my code > > result = JSON.parse(response.body) > > result.each do |key,value| > > #if k.has_value?'CHECK_NRPE: Socket timeout > after 10 seconds' > puts > "-----------------------------------------------" > puts key["host_display_name"] > #reference properties like this > puts key["plugin_output"] # this is > the result in object form > puts key["display_name"] > puts key["last_time_critical"] > puts > "------------------------------------------------" > #end > end > > > Basically the output is like : > > myserver.home.lan > CHECK_NRPE: Socket timeout after 10 seconds. > Memory Load > 1355046656 > > What i am after is > > if key["plugin_output"] == "CHECK_NRPE: Socket timeout after 10 > seconds." > puts key["host_display_name"] #reference properties like this > end > > but that doe not work > also if i try > > if result.has_value?'CHECK MAINT: MAINTANANCE MODE ENABLED ON HOST' > stil it through the error > > Can any one please give me some light on this > Thanks Well, let see if I understand, meaby you are confused. Judging by your code, the result variable points to a #Hash, so: #result = {key => value, ..} So, if I'm in the right way then I realize that each key is also a #Hash, because you say: #key["host_display_name"] So the result variable appear to be something like this: #result = {#Hash => value, ..} Meaby the values are the Hash and the key is another object, like a symbol. I didn't played with JSON, don't know how it works, but if my aproach is right instead of do #key["host_display_name"] you have to do: #value["host_display_name"] If the result variable is something like this: #result = {:some_key => {"host_display_name" => #Object}, ..} -- Posted via http://www.ruby-forum.com/.