Searching a hash by value

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

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 keys are 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}, …}

#Hash, because you say:

#key[“host_display_name”]

So the result variable appear to be something like this:

#result = {#Hash => value, …}

yes that right , a sample out put is like

   "notes" : "HP Raid Check",
  "host_notifications_enabled" : 1,
  "host_active_checks_enabled" : 1,
  "icon_image" : "",
  "check_interval" : 60,

So if i say puts key[“notes”] : the value will be HP raid Check

but will this work : #value[“host_display_name”] ??
I will try in a bit …

On Sun, Dec 9, 2012 at 1:35 PM, Ferdous ara [email protected]
wrote:

but will this work : #value[“host_display_name”] ??
I will try in a bit …

No. For lookup by value you can use Hash#rassoc:

irb(main):003:0> h={“foo” => “bar”}
=> {“foo”=>“bar”}
irb(main):004:0> h.rassoc “bar”
=> [“foo”, “bar”]
irb(main):005:0> key, val = h.rassoc “bar”
=> [“foo”, “bar”]
irb(main):006:0> key
=> “foo”

Be warned though that this may be inefficient. You probably want a
second Hash keyed by value.

Kind regards

robert

may be i need to design my logic in a different way
let me explain

basically in that hash table (out put of JSON)

as key => value

i got like this
[
{
“notes_expanded” : “check_maintanance_mode”,
“host_comments” : [],
“host_icon_image_expanded” : “”,
“checks_enabled” : 1,
“max_check_attempts” : 3,
“host” : service2.home.lan,

}
{
“notes_expanded” : "check_memory ",
“host_comments” : [],
“host_icon_image_expanded” : “”,
“checks_enabled” : 1,
“max_check_attempts” : 3,
“host” : service.home.lan,

}
{
“notes_expanded” : “check_maintanance_mode”,
“host_comments” : [],
“host_icon_image_expanded” : “”,
“checks_enabled” : 1,
“max_check_attempts” : 3,
“host” : service.home.lan,

}
]

So Basically i want to get all the host which
notes_expanded = check_maintanance_mode
so i should get only 2

Does it make sense ??

data = [
{
“notes_expanded” => “check_maintenance_mode”,
“host_comments” => [],
“host_icon_image_expanded” => “”,
“checks_enabled” => 1,
“max_check_attempts” => 3,
“host” => “service2.home.lan”,

},
{
“notes_expanded” => "check_memory ",
“host_comments” => [],
“host_icon_image_expanded” => “”,
“checks_enabled” => 1,
“max_check_attempts” => 3,
“host” => “service.NOT_THIS_ONE.home.lan”,
},
{
“notes_expanded” => “check_maintenance_mode”,
“host_comments” => [],
“host_icon_image_expanded” => “”,
“checks_enabled” => 1,
“max_check_attempts” => 3,
“host” => “service.home.lan”,
},
]

results = []

data.each do |hash|
if hash[“notes_expanded”] == “check_maintenance_mode”
results << hash[“host”]
end
end

p results

–output:–
[“service2.home.lan”, “service.home.lan”]

On Sun, Dec 9, 2012 at 2:21 PM, Ferdous ara [email protected]
wrote:

[
“notes_expanded” : "check_memory ",
“host_icon_image_expanded” : “”,

Does it make sense ??

Yes, but it’s something completely different than selecting from a
Hash based on values. You rather want something like

items = stuff.select {|h| h[“notes_expanded”] ==
“check_maintanance_mode”}

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs