2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,11 @@
|
|||
In this task, the goal is to create an [[associative array]] (also known as a dictionary, map, or hash).
|
||||
;Task:
|
||||
The goal is to create an [[associative array]] (also known as a dictionary, map, or hash).
|
||||
|
||||
|
||||
Related tasks:
|
||||
* [[Associative arrays/Iteration]]
|
||||
* [[Hash from two arrays]]
|
||||
|
||||
|
||||
{{Template:See also lists}}
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
[map
|
||||
("foo" 13)
|
||||
("bar" 42)
|
||||
("baz" 77)]
|
||||
(("foo" 13)
|
||||
("bar" 42)
|
||||
("baz" 77)) ls2map !
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
defmodule RC do
|
||||
def dict_create(dict_impl \\ Map) do
|
||||
d = dict_impl.new #=> creates an empty Dict
|
||||
d1 = Dict.put(d,:foo,1)
|
||||
d2 = Dict.put(d1,:bar,2)
|
||||
print_vals(d2)
|
||||
print_vals(Dict.put(d2,:foo,3))
|
||||
def test_create do
|
||||
IO.puts "< create Map.new >"
|
||||
m = Map.new #=> creates an empty Map
|
||||
m1 = Map.put(m,:foo,1)
|
||||
m2 = Map.put(m1,:bar,2)
|
||||
print_vals(m2)
|
||||
print_vals(%{m2 | foo: 3})
|
||||
end
|
||||
|
||||
defp print_vals(d) do
|
||||
IO.inspect d
|
||||
Enum.each(d, fn {k,v} -> IO.puts "#{k}: #{v}" end)
|
||||
defp print_vals(m) do
|
||||
IO.inspect m
|
||||
Enum.each(m, fn {k,v} -> IO.puts "#{inspect k} => #{v}" end)
|
||||
end
|
||||
end
|
||||
|
||||
IO.puts "< create Map.new >"
|
||||
RC.dict_create
|
||||
IO.puts "\n< create HashDict.new >"
|
||||
RC.dict_create(HashDict)
|
||||
RC.test_create
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
map.get("foo"); // => 5
|
||||
map.get("foo"); // => 6
|
||||
map.get("invalid"); // => null
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
fun main(args: Array<String>) {
|
||||
// map definition:
|
||||
val map = mapOf("foo" to 5,
|
||||
"bar" to 10,
|
||||
"baz" to 15,
|
||||
"foo" to 6)
|
||||
|
||||
// retrieval:
|
||||
println(map["foo"]) // => 6
|
||||
println(map["invalid"]) // => null
|
||||
|
||||
// check keys:
|
||||
println("foo" in map) // => true
|
||||
println("invalid" in map) // => false
|
||||
|
||||
// iterate over keys:
|
||||
for (k in map.keys) print("$k ")
|
||||
println()
|
||||
|
||||
// iterate over values:
|
||||
for (v in map.values) print("$v ")
|
||||
println()
|
||||
|
||||
// iterate over key, value pairs:
|
||||
for ((k, v) in map) println("$k => $v")
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
use std::collections::HashMap;
|
||||
fn main() {
|
||||
let mut olympic_medals = HashMap::new();
|
||||
olympic_medals.insert("United States", (1072, 859, 749));
|
||||
olympic_medals.insert("Soviet Union", (473, 376, 355));
|
||||
olympic_medals.insert("Great Britain", (246, 276, 284));
|
||||
olympic_medals.insert("Germany", (252, 260, 270));
|
||||
println!("{:?}", olympic_medals);
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
m := {['foo', 'a'], ['bar', 'b'], ['baz', 'c']};
|
||||
|
|
@ -0,0 +1 @@
|
|||
print( m('bar') );
|
||||
|
|
@ -0,0 +1 @@
|
|||
print( m{'bar'} );
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
Option Explicit
|
||||
Sub Test()
|
||||
Dim h As Object
|
||||
Set h = CreateObject("Scripting.Dictionary")
|
||||
h.Add "A", 1
|
||||
h.Add "B", 2
|
||||
h.Add "C", 3
|
||||
Debug.Print h.Item("A")
|
||||
h.Item("C") = 4
|
||||
h.Key("C") = "D"
|
||||
Debug.Print h.exists("C")
|
||||
h.Remove "B"
|
||||
Debug.Print h.Count
|
||||
h.RemoveAll
|
||||
Debug.Print h.Count
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue