View Problem

Remove an entry from a map

Given a map pets {joe:cat,mary:turtle,bill:canary} remove the mapping for "bill" and print "canary"
DiskEdit
clojure
; Maps are immutable
; The following expression will return a new map without the 'bill key
(let [pets '{joe cat mary turtle bill canary}]
(println (get pets 'bill))
(dissoc pets 'bill))
ExpandDiskEdit
cpp C++/CLI .NET 2.0
if (pets->ContainsKey("bill"))
{
String^ value = safe_cast<String^>(pets["bill"]); pets->Remove("bill");
Console::WriteLine("{0}", value);
}
ExpandDiskEdit
erlang
Pet = dict:fetch(bill, Pets0), Pets1 = dict:erase(bill, Pets0), io:format("~w~n", [Pet]),
ExpandDiskEdit
erlang
Pet = ets:lookup_element(Pets, bill, 2), ets:delete(Pets, bill), io:format("~w~n", [Pet]),
ExpandDiskEdit
erlang
{value, Pet} = gb_trees:lookup(bill, Pets0), Pets1 = gb_trees:delete(bill, Pets0), io:format("~w~n", [Pet]),
ExpandDiskEdit
fantom
pet := map.remove("bill")
echo ("pet=$pet")
ExpandDiskEdit
fsharp
let key = "bill"
match (pets |> Map.tryFind key) with
| Some(value) -> pets <- (Map.remove key pets) ; printfn "%s : %s removed" key value
| None -> printfn "Key %s not found" key
ExpandDiskEdit
fsharp
let key = "bill"
let entry = if (pets.ContainsKey(key)) then Some(pets.[key]) ; else None
pets.Remove(key)

match entry with
| Some(value) -> printfn "%s" value
| None -> printfn "key not found"
ExpandDiskEdit
go 1+
fmt.Println(pets["bill"])
delete(pets, "bill")
ExpandDiskEdit
go 0.x
fmt.Println(pets["bill"])
pets["bill"] = "", false
ExpandDiskEdit
groovy
pets = [joe:'cat', mary:'turtle', bill:'canary']
println pets.remove('bill')
DiskEdit
haskell
import qualified Data.Map as M

main = do
let pets = M.fromList [("joe", "cat"), ("mary", "turtle"), ("bill", "canary")]
pets2 = M.delete "bill" pets
print $ maybe "" id (M.lookup "bill" pets)
print pets2
ExpandDiskEdit
java
System.out.println(pets.remove("bill"))
DiskEdit
ocaml
module StringMap = Map.Make (String)

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

let get_and_rem key m =
try
let value = StringMap.find key m in
let rm = StringMap.remove key m in
Some (value, rm)
with Not_found ->
None

let () =
let key = "bill" in
match get_and_rem key pets with
| Some (found, new_pets) ->
Printf.printf "%s : %s removed\n" key found
| None ->
Printf.printf "Key %s not found" key
DiskEdit
ocaml
let get_and_rem m key =
try
let value = Hashtbl.find m key in
Hashtbl.remove m key;
Some value
with Not_found ->
None

let () =
let pets = Hashtbl.create 42 in
List.iter (fun (key, value) ->
Hashtbl.add pets key value
) [("joe", "cat"); ("mary", "turtle"); ("bill", "canary")];

let key = "bill" in
match get_and_rem pets key with
| Some found ->
Printf.printf "%s : %s removed\n" key found
| None ->
Printf.printf "Key %s not found" key
DiskEdit
perl
print delete $pets{bill};
ExpandDiskEdit
php
$last = array_pop($pets); // removes last key and assign it to the variable
echo $last;
ExpandDiskEdit
php
$last = $pets[count($pets)-1];
echo $last;
unset($last);
ExpandDiskEdit
php
$bill = $pets["bill"];
echo $bill;
unset($bill);
ExpandDiskEdit
php
print_r(array_pop($pets));
DiskEdit
python
print pets.pop('bill')
ExpandDiskEdit
ruby
puts map.delete :bill
ExpandDiskEdit
scala
val pet = pets("bill") ; pets -= "bill" ; println(pet)
ExpandDiskEdit
scala
println(pets.removeKey("bill") match { case Some(pet) => pet ; case None => "***" })

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