This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,3 +1,3 @@
Show how to iterate over the key-value pairs of an associative array, and print each pair out. Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.
* Related task: [[Associative arrays/Creation]]
{{Template:See also lists}}

View file

@ -0,0 +1,12 @@
record r;
text s;
r_put(r, "A", 33); # an integer value
r_put(r, "C", 2.5); # a real value
r_put(r, "B", "associative"); # a string value
if (r_first(r, s)) {
do {
o_plan("key ", s, ", value ", r_query(r, s), " (", r_type(r, s), ")\n");
} while (r_greater(r, s, s));
}

View file

@ -0,0 +1,10 @@
var A = [ "H2O" => "water", "NaCl" => "salt", "O2" => "oxygen" ];
for k in A.domain do
writeln("have key: ", k);
for v in A do
writeln("have value: ", v);
for (k,v) in zip(A.domain, A) do
writeln("have element: ", k, " -> ", v);

View file

@ -1,26 +1,19 @@
#define std'dictionary'*.
#define std'routines'*.
#define std'collections'*.
#define std'patterns'*.
#define system.
#define system'collections.
// --- Program ---
#symbol Program =
#symbol program =
[
// 1. Create
#var aMap := Dictionary.
aMap
append &dictionary_key:"key" &content:"foox"
append &dictionary_key:"key" &content:"foo"
append &dictionary_key:"key2" &content:"foo2"
append &dictionary_key:"key3" &content:"foo3"
append &dictionary_key:"key4" &content:"foo4".
#var aMap := Dictionary new.
aMap @ "key" << "foox".
aMap @ "key" << "foo".
aMap @ "key2" << "foo2".
aMap @ "key3" << "foo3".
aMap @ "key4" << "foo4".
(aMap enumerator)~foreach run: anItem =
[
'program'Output << anItem dictionary_key << " : " << anItem content << "%n".
].
// only values
Scan::aMap run: aValue = ('program'Output << avalue << "%n").
// Enumerate
control foreach:aMap &do: &&:aKeyValue
[ console write:(aKeyValue Key) write:" : " writeLine:(aKeyValue Value) ].
].

View file

@ -0,0 +1,11 @@
h[1]: 6$
h[9]: 2$
/* iterate over values */
for val in listarray(h) do (
print(val))$
/* iterate over the keys */
for key in rest(arrayinfo(h), 2) do (
val: arrayapply(h, key),
print(key, val))$

View file

@ -1,13 +1,7 @@
val m=Map("Hello"->13, "world"->31, "!"->71)
val m = Map("Amsterdam" -> "Netherlands", "New York" -> "USA", "Heemstede" -> "Netherlands")
println("Keys:")
m.keys foreach println
println("\nValues:")
m.values foreach println
println("\nPairs:")
m foreach println
println("\nKey->Value:")
for((k,v)<-m) println(k+"->"+v)
println(f"Key->Value: ${m.mkString(", ")}%s")
println(f"Pairs: ${m.toList.mkString(", ")}%s")
println(f"Keys: ${m.keys.mkString(", ")}%s")
println(f"Values: ${m.values.mkString(", ")}%s")
println(f"Unique values: ${m.values.toSet.mkString(", ")}%s")