View Problem

Retrieve a value from a map

Given a map pets {joe:cat,mary:turtle,bill:canary} print the pet for "joe" ("cat")
ExpandDiskEdit
ruby
puts map['joe']
ExpandDiskEdit
erlang
dict:is_key(joe, Pets) andalso begin io:format("~w~n", [dict:fetch(joe, Pets)]), true end.
ExpandDiskEdit
erlang
case dict:find(joe, Pets) of error -> false ; {ok, Pet} -> io:format("~w~n", [Pet]) end.
ExpandDiskEdit
erlang
IsMember = ets:member(Pets, joe), if (IsMember) -> io:format("~w~n", [ets:lookup_element(Pets, joe, 2)]) ; true -> false end.
ExpandDiskEdit
erlang
case ets:match(Pets, {joe, '$1'}) of [] -> false ; [[Pet]] -> io:format("~w~n", [Pet]) end.
ExpandDiskEdit
erlang
case gb_trees:lookup(joe, Pets) of none -> false ; {value, Pet} -> io:format("~w~n", [Pet]) end.
DiskEdit
clojure
(def pets '{joe cat mary turtle bill canary})

(println (get pets 'joe))
ExpandDiskEdit
fsharp
if (Map.mem "joe" pets) then printfn "%s" (Map.find "joe" pets)
ExpandDiskEdit
fsharp
if (pets |> Map.exists (fun key _ -> key = "joe")) then printfn "%s" (Map.find "joe" pets)
ExpandDiskEdit
fsharp
let key = "joe"
match (pets |> Map.tryfind key) with
| Some(value) -> printfn "%s" value
| None -> printfn "Key %s not found" key
ExpandDiskEdit
fsharp
if pets.ContainsKey("joe") then printfn "%s" pets.["joe"]
ExpandDiskEdit
fsharp
if pets.ContainsKey("joe") then printfn "%s" (pets.["joe"] :?> string)

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