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
cpp C++/CLI .NET 2.0
if (pets->ContainsKey("joe")) Console::WriteLine(pets["joe"]);
ExpandDiskEdit
cpp
cout << pets["joe"] << endl;
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)
DiskEdit
clojure
(def pets '{joe cat mary turtle bill canary})

(println (get pets 'joe))

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