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
erlang
dict:is_key(mary, Pets) andalso begin io:format("ok~n"), true end.
ExpandDiskEdit
erlang
IsMember = ets:member(Pets, mary), if (IsMember) -> io:format("ok~n") ; true -> false end.
ExpandDiskEdit
erlang
case gb_trees:lookup(mary, Pets) of none -> false ; _ -> io:format("ok~n") end.
DiskEdit
clojure
(if (contains? '{joe cat mary turtle bill canary} 'mary)
(println "ok"))
ExpandDiskEdit
go
m := map[string]string{ "joe": "cat", "mary": "turtle", "bill": "canary" }
if _, ok := m["mary"]; ok {
fmt.Println("ok")
}
ExpandDiskEdit
fantom
map := ["joe":"cat", "mary":"turtle", "bill":"canary"]
if (map.containsKey("mary")) echo("ok")

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