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
clojure
(if (contains? '{joe cat mary turtle bill canary} 'mary)
(println "ok"))
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
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.
ExpandDiskEdit
fantom
map := ["joe":"cat", "mary":"turtle", "bill":"canary"]
if (map.containsKey("mary")) echo("ok")
ExpandDiskEdit
fsharp
if (Map.mem "mary" pets) then printfn "ok"
ExpandDiskEdit
fsharp
if pets.ContainsKey("mary") then printfn "ok"
ExpandDiskEdit
go
m := map[string]string{ "joe": "cat", "mary": "turtle", "bill": "canary" }
if _, ok := m["mary"]; ok {
fmt.Println("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'
DiskEdit
haskell
import qualified Data.Map as M
import Control.Monad (when)

pets = M.fromList [("joe", "cat"), ("mary", "turtle"), ("bill", "canary")]

checkMary = when (M.member "mary" pets) (print "ok")

ExpandDiskEdit
java
if (pets.containsKey("mary")) System.out.println("ok");
DiskEdit
ocaml
module StringMap = Map.Make (String)

let map =
List.fold_left (fun map (key, value) ->
StringMap.add key value map
) StringMap.empty [("joe", "cat"); ("mary", "turtle"); ("bill", "canary")]

let () =
if StringMap.mem "mary" map
then print_endline "OK"
DiskEdit
ocaml
let () =
let map = Hashtbl.create 42 in
List.iter (fun (key, value) ->
Hashtbl.add map key value
) [("joe", "cat"); ("mary", "turtle"); ("bill", "canary")];

if Hashtbl.mem map "mary" then print_endline "OK"
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}
ExpandDiskEdit
php
if (array_key_exists("mary", $pets)) {
echo "ok";
}
ExpandDiskEdit
php
if (isset($pets["mary"])) { // only works if $pets["mary"] can't be false or 0
echo "ok";
}
DiskEdit
python
pets = dict(joe='cat', mary='turtle', bill='canary')
if ("mary" in pets) print "ok"
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
scala
if (pets.contains("mary")) println("ok")
DiskEdit
scala
map.get("mary").foreach(println("ok"))

Submit a new solution for clojure, cpp, erlang, fantom ...