View Problem

Check if a key exists in a map

Given a map pets {joe:cat,mary:turtle,bill:canary} print "ok" if an pet exists for "mary"
ExpandDiskEdit
ruby
puts "ok" if map.has_key?('mary')
ExpandDiskEdit
ruby
puts "ok" if map['mary'] # Only works if map entry can't be nil or false
ExpandDiskEdit
cpp C++/CLI .NET 2.0
if (pets->ContainsKey("mary")) Console::WriteLine("ok");
ExpandDiskEdit
cpp
if (pets.find("mary") != pets.end()){
std::cout << "ok" << std::endl;
}
ExpandDiskEdit
cpp
if (pets.count("mary") > 0)
cout << "ok" << endl;
ExpandDiskEdit
fsharp
if (Map.mem "mary" pets) then printfn "ok"
ExpandDiskEdit
fsharp
if pets.ContainsKey("mary") then printfn "ok"
DiskEdit
groovy
pets = [joe:'cat', mary:'turtle', bill:'canary']
if(pets.containsKey('mary')) println 'ok'
DiskEdit
groovy
pets = [joe:'cat', mary:'turtle', bill:'canary']
if(pets.mary) println 'ok'

Submit a new solution for ruby, cpp, fsharp, or groovy
There are 19 other solutions in additional languages (clojure, erlang, fantom, go ...)