View Problem

Retrieve a value from a map

Given a map pets {joe:cat,mary:turtle,bill:canary} print the pet for "joe" ("cat")
DiskEdit
clojure
(def pets '{joe cat mary turtle bill canary})

(println (get pets 'joe))
ExpandDiskEdit
cpp C++/CLI .NET 2.0
if (pets->ContainsKey("joe")) Console::WriteLine(pets["joe"]);
ExpandDiskEdit
cpp
cout << pets["joe"] << endl;
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.
ExpandDiskEdit
fantom
map := ["joe":"cat", "mary":"turtle", "bill":"canary"]
pet := map["joe"]
echo("pet=$pet")
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)
ExpandDiskEdit
go
fmt.Println(pets["joe"])
DiskEdit
groovy
pets = [joe:'cat', mary:'turtle', bill:'canary']
assert pets['joe'] == 'cat'
ExpandDiskEdit
groovy
assert pets.joe == 'cat'
DiskEdit
haskell
import qualified Data.Map as M

pets = M.fromList [("joe", "cat"), ("mary", "turtle"), ("bill", "canary")]
retrieve = print $ M.findWithDefault "Not found" "joe" pets
ExpandDiskEdit
java
String pet = pets.get("joe");
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 () =
try
let pet = StringMap.find "joe" map in
Printf.printf "Joe's pet is a %s.\n" pet
with Not_found ->
prerr_endline "No pet found for Joe."
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")];

try
let pet = Hashtbl.find map "joe" in
Printf.printf "Joe's pet is a %s.\n" pet
with Not_found ->
prerr_endline "No pet found for Joe."
DiskEdit
perl
%pets = (joe => 'cat', mary => 'turtle', bill=>'canary');
print $pets{joe};
ExpandDiskEdit
php
echo $pets["joe"];
DiskEdit
python
print pets['joe']
ExpandDiskEdit
ruby
puts map['joe']
ExpandDiskEdit
scala
if (pets.contains("joe")) println(pets("joe"))
ExpandDiskEdit
scala
println(pets.getOrElse("joe", "*** no pet owned ***"))
DiskEdit
scala
pets("joe")

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