how to know Array if have key?

Z

Zhenning Guan

a_array= [{"1"=>"11"}, {"2"=>"22"}]
how to test if a_array have a key "11"????
 
C

Clifford Heath

Zhenning said:
a_array= [{"1"=>"11"}, {"2"=>"22"}]
how to test if a_array have a key "11"????

Arrays don't have keys, Hashes do.
Here, you have an array with two elements.
Both the elements are hashes. Neither hash has
the key "11", though the first has it as a value.

If you want to know whether a hash has a value,
it's slow; you have to search the hash, or extract
all the values and check for the one you care about.
 
C

Chris Howe

[Note: parts of this message were removed to make it a legal post.]

If you have a hash and you want to find whether or not a given value exists
as a key you can use the "has_key?" method

h = { "a" => 100, "b" => 200 }
h.has_key?("a") #=> true
h.has_key?("z") #=> false
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top