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"
DiskEdit
python
pets = dict(joe='cat', mary='turtle', bill='canary')
if ("mary" in pets) print "ok"
DiskEdit
clojure
(if (contains? '{joe cat mary turtle bill canary} 'mary)
(println "ok"))
ExpandDiskEdit
fsharp
if (Map.mem "mary" pets) then printfn "ok"
ExpandDiskEdit
fsharp
if pets.ContainsKey("mary") then printfn "ok"
ExpandDiskEdit
fantom
map := ["joe":"cat", "mary":"turtle", "bill":"canary"]
if (map.containsKey("mary")) echo("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'
ExpandDiskEdit
java
if (pets.containsKey("mary")) System.out.println("ok");
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
perl
%pets = (joe => 'cat', mary => 'turtle', bill => 'canary');
print 'ok' if ($pets{'mary'});
DiskEdit
perl
%pets = (joe => 'cat', mary => 'turtle', bill => 'canary');
print 'ok' if $pets{'mary'};
DiskEdit
perl
print 'ok' if $pets{mary};
DiskEdit
perl
print 'ok' if exists $pets{mary}

Submit a new solution for python, clojure, fsharp, fantom ...
There are 13 other solutions in additional languages (cpp, go, haskell, ocaml ...)