Associative array/Iteration: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
 
(39 intermediate revisions by 28 users not shown)
Line 1: Line 1:
{{task|Basic language learning}}[[Category:Iteration]][[Category:Data Structures]]
[[Category:Iteration]]
[[Category:Data Structures]]
Show how to iterate over the key-value pairs of an associative array, and print each pair out.
{{task|Basic language learning}}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.
Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.
Line 9: Line 10:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>V d = [‘key1’ = ‘value1’, ‘key2’ = ‘value2’]
<syntaxhighlight lang="11l">V d = [‘key1’ = ‘value1’, ‘key2’ = ‘value2’]


L(key, value) d
L(key, value) d
Line 18: Line 19:


L(value) d.values()
L(value) d.values()
print(value)</lang>
print(value)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 31: Line 32:
=={{header|8th}}==
=={{header|8th}}==
Iterating key,value pairs uses "m:each":
Iterating key,value pairs uses "m:each":
<syntaxhighlight lang="forth">
<lang Forth>
{"one": 1, "two": "bad"}
{"one": 1, "two": "bad"}
( swap . space . cr )
( swap . space . cr )
m:each
m:each
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
one 1
one 1
Line 42: Line 43:


Iterating the keys uses "m:keys":
Iterating the keys uses "m:keys":
<syntaxhighlight lang="forth">
<lang Forth>
{"one": 1, "two": "bad"} m:keys
{"one": 1, "two": "bad"} m:keys
( . cr )
( . cr )
a:each
a:each
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
one
one
Line 53: Line 54:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Containers.Indefinite_Ordered_Maps;


Line 71: Line 72:
Index := Next (Index);
Index := Next (Index);
end loop;
end loop;
end Test_Iteration;</lang>
end Test_Iteration;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 80: Line 81:


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>record r;
<syntaxhighlight lang="aime">record r;
text s;
text s;


Line 91: Line 92:
o_form("key ~, value ~ (~)\n", s, r[s], r_type(r, s));
o_form("key ~, value ~ (~)\n", s, r[s], r_type(r, s));
} while (rsk_greater(r, s, s));
} while (rsk_greater(r, s, s));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>key A, value 33 (integer)
<pre>key A, value 33 (integer)
Line 101: Line 102:
<br>
<br>
This sample defines a simple hash-based implementation with operators to iterate over the array.
This sample defines a simple hash-based implementation with operators to iterate over the array.
<lang algol68># associative array handling using hashing #
<syntaxhighlight lang="algol68"># associative array handling using hashing #


# the modes allowed as associative array element values - change to suit #
# the modes allowed as associative array element values - change to suit #
Line 255: Line 256:
e := NEXT a1
e := NEXT a1
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 271: Line 272:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang arturo>// create a dictionary
<syntaxhighlight lang="rebol">; create a dictionary
dict: #{
d: #[
name: "john"
name: "john"
surname: "doe"
surname: "doe"
age: 33
age: 34
]
}


// Iterate over key/value pairs
; Iterate over key/value pairs
loop dict {
loop d [key,value][
print "key = " + &0 + ", value = " + &1
print ["key =" key ", value =" value]
]
}


print "----"
print "----"


// Iterate over keys
; Iterate over keys
loop [keys dict] {
loop keys d [k][
print "key = " + &
print ["key =" k]
]
}


print "----"
print "----"


// Iterate over values
; Iterate over values
loop [values dict] {
loop values d [v][
print "value = " + &
print ["value =" v]
]</syntaxhighlight>
}</lang>


{{out}}
{{out}}


<pre>key = surname, value = doe
<pre>key = name , value = john
key = age, value = 33
key = surname , value = doe
key = name, value = john
key = age , value = 34
----
----
key = surname
key = name
key = age
key = surname
key = name
key = age
----
----
value = doe
value = john
value = 33
value = doe
value = john</pre>
value = 34</pre>

=={{header|ATS}}==
See [[Associative_array/Creation#ATS|Associative_array/Creation#ATS]].


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
{{works with|AutoHotkey_L}}
From the [http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/objects/Enumerator.htm documentation]<lang AutoHotkey>; Create an associative array
From the [http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/objects/Enumerator.htm documentation]<syntaxhighlight lang="autohotkey">; Create an associative array
obj := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
obj := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
enum := obj._NewEnum()
enum := obj._NewEnum()
While enum[key, value]
While enum[key, value]
t .= key "=" value "`n"
t .= key "=" value "`n"
MsgBox % t</lang>
MsgBox % t</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
In AWK "arrays" are always associative arrays, and the only way to iterate over them is by keys (''indexes'' in the AWK terminology)
In AWK "arrays" are always associative arrays, and the only way to iterate over them is by keys (''indexes'' in the AWK terminology), in undefined order.
<syntaxhighlight lang="awk">BEGIN {

<lang awk>BEGIN {
a["hello"] = 1
a["hello"] = 1
a["world"] = 2
a["world"] = 2
a["!"] = 3
a["!"] = 3


# iterate over keys
# iterate over keys, undefined order
for(key in a) {
print key, a[key]
}
}</syntaxhighlight>

As AWK was often used in (Bourne) shell scripts,
sorting was done by a pipe of two awk programs and the sort command.
Today, 'gawk' allows to set the order of iteration:
<syntaxhighlight lang="awk">BEGIN {
a["hello"] = 1
a["world"] = 2
a["!"] = 3
PROCINFO["sorted_in"] = "@ind_str_asc" # controls index order
# iterate over keys, indices as strings sorted ascending
for(key in a) {
for(key in a) {
print key, a[key]
print key, a[key]
}
}
}</lang>
}</syntaxhighlight>


=={{header|Babel}}==
=={{header|Babel}}==
Line 338: Line 355:
In Babel, associative arrays are referred to as maps. To create a map from a list-of-lists:
In Babel, associative arrays are referred to as maps. To create a map from a list-of-lists:


<lang babel>births (('Washington' 1732) ('Lincoln' 1809) ('Roosevelt' 1882) ('Kennedy' 1917)) ls2map ! <</lang>
<syntaxhighlight lang="babel">births (('Washington' 1732) ('Lincoln' 1809) ('Roosevelt' 1882) ('Kennedy' 1917)) ls2map ! <</syntaxhighlight>


To iterate over a map, in the primary sense, use the overmap utility. We will copy the map (cp operator) so as not to modify the original:
To iterate over a map, in the primary sense, use the overmap utility. We will copy the map (cp operator) so as not to modify the original:


<lang babel>births cp dup {1 +} overmap !</lang>
<syntaxhighlight lang="babel">births cp dup {1 +} overmap !</syntaxhighlight>


To see the results, use the valmap operator:
To see the results, use the valmap operator:


<lang babel>valmap ! lsnum !</lang>
<syntaxhighlight lang="babel">valmap ! lsnum !</syntaxhighlight>


{{out}}
{{out}}
Line 353: Line 370:
There are many ways to interact with a map in Babel. Most of these begin by converting the map to a list or list-of-lists. To look up a list of specific values from the map, by key, use the lumapls utility:
There are many ways to interact with a map in Babel. Most of these begin by converting the map to a list or list-of-lists. To look up a list of specific values from the map, by key, use the lumapls utility:


<lang babel>births ('Roosevelt' 'Kennedy') lumapls ! lsnum !</lang>
<syntaxhighlight lang="babel">births ('Roosevelt' 'Kennedy') lumapls ! lsnum !</syntaxhighlight>


{{out}}
{{out}}
Line 360: Line 377:
To convert the entire map back to a list of key-value pairs:
To convert the entire map back to a list of key-value pairs:


<lang babel>births map2ls !</lang>
<syntaxhighlight lang="babel">births map2ls !</syntaxhighlight>


To view the list:
To view the list:


<lang babel>{give swap << " " << itod << "\n" <<} each</lang>
<syntaxhighlight lang="babel">{give swap << " " << itod << "\n" <<} each</syntaxhighlight>


{{out}}
{{out}}
Line 374: Line 391:
To merge two maps together, use the mapmerge utility:
To merge two maps together, use the mapmerge utility:


<lang babel>foo (("bar" 17) ("baz" 42)) ls2map ! <
<syntaxhighlight lang="babel">foo (("bar" 17) ("baz" 42)) ls2map ! <
births foo mergemap !</lang>
births foo mergemap !</syntaxhighlight>


To view the results:
To view the results:


<lang babel>births map2ls ! {give swap << " " << itod << "\n" <<} each</lang>
<syntaxhighlight lang="babel">births map2ls ! {give swap << " " << itod << "\n" <<} each</syntaxhighlight>


{{out}}
{{out}}
Line 391: Line 408:
For more information on maps in Babel, view [https://github.com/claytonkb/clean_babel/blob/master/std.sp std.sp] (see the section titled "map utilities").
For more information on maps in Babel, view [https://github.com/claytonkb/clean_babel/blob/master/std.sp std.sp] (see the section titled "map utilities").


=={{header|BaCon}}==
=={{header|BASIC}}==
==={{header|BaCon}}===
<lang qbasic>DECLARE associative ASSOC STRING
<syntaxhighlight lang="qbasic">DECLARE associative ASSOC STRING


associative("abc") = "first three"
associative("abc") = "first three"
Line 401: Line 419:
FOR i = 0 TO amount - 1
FOR i = 0 TO amount - 1
PRINT keys$[i], ":", associative(keys$[i])
PRINT keys$[i], ":", associative(keys$[i])
NEXT</lang>
NEXT</syntaxhighlight>


{{out}}
{{out}}
Line 410: Line 428:
LOOKUP creates a numerically indexed array of the keys of the associative array, with the number of elements stored in the field following the SIZE keyword.
LOOKUP creates a numerically indexed array of the keys of the associative array, with the number of elements stored in the field following the SIZE keyword.


=={{header|BASIC256}}==
==={{header|BASIC256}}===
''Solution is at [[Associative_array/Creation#BASIC256]]''.
''Solution is at [[Associative_array/Creation#BASIC256]]''.


=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
<lang bbcbasic> REM Store some values with their keys:
<syntaxhighlight lang="bbcbasic"> REM Store some values with their keys:
PROCputdict(mydict$, "FF0000", "red")
PROCputdict(mydict$, "FF0000", "red")
PROCputdict(mydict$, "00FF00", "green")
PROCputdict(mydict$, "00FF00", "green")
Line 439: Line 457:
key$ = MID$(dict$, J%+1, K%-J%-1)
key$ = MID$(dict$, J%+1, K%-J%-1)
IF K% >= LEN(dict$) THEN K% = 0
IF K% >= LEN(dict$) THEN K% = 0
= K%</lang>
= K%</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>( new$hash:?myhash
<syntaxhighlight lang="bracmat">( new$hash:?myhash
& (myhash..insert)$(title."Some title")
& (myhash..insert)$(title."Some title")
& (myhash..insert)$(formula.a+b+x^7)
& (myhash..insert)$(formula.a+b+x^7)
Line 459: Line 477:
& put$\n
& put$\n
)
)
);</lang>
);</syntaxhighlight>
{{out}}
{{out}}
<pre>key: meat
<pre>key: meat
Line 477: Line 495:


=={{header|Brat}}==
=={{header|Brat}}==
<lang brat>h = [ hello: 1 world: 2 :! : 3]
<syntaxhighlight lang="brat">h = [ hello: 1 world: 2 :! : 3]


#Iterate over key, value pairs
#Iterate over key, value pairs
Line 492: Line 510:
h.each_value { v |
h.each_value { v |
p "Value: #{v}"
p "Value: #{v}"
}</lang>
}</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
''Solution is at [[Associative arrays/Creation/C]]''.
''Solution is at [[Associative arrays/Creation/C]]''.


=={{header|C sharp}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 531: Line 549:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
{{works with|C++11}}
{{works with|C++11}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <map>
#include <string>
#include <string>
Line 555: Line 573:


return 0;
return 0;
}</lang>
}</syntaxhighlight>




Pre C++11:
Pre C++11:
<lang cpp>std::map<std::string, int> myDict;
<syntaxhighlight lang="cpp">std::map<std::string, int> myDict;
myDict["hello"] = 1;
myDict["hello"] = 1;
myDict["world"] = 2;
myDict["world"] = 2;
Line 570: Line 588:
int& value = it->second;
int& value = it->second;
std::cout << "key = " << key << ", value = " << value << std::endl;
std::cout << "key = " << key << ", value = " << value << std::endl;
}</lang>
}</syntaxhighlight>


=={{header|Ceylon}}==
=={{header|Ceylon}}==
<lang ceylon>shared void run() {
<syntaxhighlight lang="ceylon">shared void run() {


value myMap = map {
value myMap = map {
Line 593: Line 611:
}
}
}</lang>
}</syntaxhighlight>


=={{header|Chapel}}==
=={{header|Chapel}}==


<lang chapel>var A = [ "H2O" => "water", "NaCl" => "salt", "O2" => "oxygen" ];
<syntaxhighlight lang="chapel">var A = [ "H2O" => "water", "NaCl" => "salt", "O2" => "oxygen" ];


for k in A.domain do
for k in A.domain do
Line 606: Line 624:


for (k,v) in zip(A.domain, A) do
for (k,v) in zip(A.domain, A) do
writeln("have element: ", k, " -> ", v);</lang>
writeln("have element: ", k, " -> ", v);</syntaxhighlight>


{{out}}
{{out}}
Line 620: Line 638:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>
<syntaxhighlight lang="clojure">
(doseq [[k v] {:a 1, :b 2, :c 3}]
(doseq [[k v] {:a 1, :b 2, :c 3}]
(println k "=" v))
(println k "=" v))
Line 629: Line 647:
(doseq [v (vals {:a 1, :b 2, :c 3})]
(doseq [v (vals {:a 1, :b 2, :c 3})]
(println v))
(println v))
</syntaxhighlight>
</lang>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<lang coffeescript>hash =
<syntaxhighlight lang="coffeescript">hash =
a: 'one'
a: 'one'
b: 'two'
b: 'two'
Line 641: Line 659:
for key of hash
for key of hash
console.log key
console.log key
</syntaxhighlight>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 650: Line 668:
The association list is a list of conses, each of whose <code>car</code> is a key and whose <code>cdr</code> is a value. The standard mapping and print functions can be used to print key/value pairs, keys, and values.
The association list is a list of conses, each of whose <code>car</code> is a key and whose <code>cdr</code> is a value. The standard mapping and print functions can be used to print key/value pairs, keys, and values.


<lang lisp>;; iterate using dolist, destructure manually
<syntaxhighlight lang="lisp">;; iterate using dolist, destructure manually
(dolist (pair alist)
(dolist (pair alist)
(destructuring-bind (key . value) pair
(destructuring-bind (key . value) pair
Line 657: Line 675:
;; iterate and destructure with loop
;; iterate and destructure with loop
(loop for (key . value) in alist
(loop for (key . value) in alist
do (format t "~&Key: ~a, Value: ~a." key value))</lang>
do (format t "~&Key: ~a, Value: ~a." key value))</syntaxhighlight>


===With property lists (plists)===
===With property lists (plists)===
Line 663: Line 681:
Property lists are lists of alternating keys and values, where each value's key is the element of the list immediately following it. Printing could be done with standard mapping functions, but <code>loop</code>'s destructuring makes things a bit easier.
Property lists are lists of alternating keys and values, where each value's key is the element of the list immediately following it. Printing could be done with standard mapping functions, but <code>loop</code>'s destructuring makes things a bit easier.


<lang lisp>(loop for (key value) on plist :by 'cddr
<syntaxhighlight lang="lisp">(loop for (key value) on plist :by 'cddr
do (format t "~&Key: ~a, Value: ~a." key value))</lang>
do (format t "~&Key: ~a, Value: ~a." key value))</syntaxhighlight>


===With hash tables===
===With hash tables===
Line 670: Line 688:
Lisp also has built-in hash tables, and there are several ways to map over these. The first is <code>maphash</code> which takes a function of two arguments (the key and value) and the hash table.
Lisp also has built-in hash tables, and there are several ways to map over these. The first is <code>maphash</code> which takes a function of two arguments (the key and value) and the hash table.


<lang lisp>(maphash (lambda (key value)
<syntaxhighlight lang="lisp">(maphash (lambda (key value)
(format t "~&Key: ~a, Value: ~a." key value))
(format t "~&Key: ~a, Value: ~a." key value))
hash-table)</lang>
hash-table)</syntaxhighlight>


The <code>loop</code> construct also supports extracting key/value pairs from hash tables.
The <code>loop</code> construct also supports extracting key/value pairs from hash tables.


<lang lisp>(loop for key being each hash-key of hash-table using (hash-value value)
<syntaxhighlight lang="lisp">(loop for key being each hash-key of hash-table using (hash-value value)
do (format t "~&Key: ~a, Value: ~a." key value))</lang>
do (format t "~&Key: ~a, Value: ~a." key value))</syntaxhighlight>


There is also a macro <code>with-hash-table-iterator</code> which locally binds a name to produce associated keys and values of the hash table; while rarely used, it is the most powerful operation.
There is also a macro <code>with-hash-table-iterator</code> which locally binds a name to produce associated keys and values of the hash table; while rarely used, it is the most powerful operation.


<lang lisp>(with-hash-table-iterator (next-entry hash-table)
<syntaxhighlight lang="lisp">(with-hash-table-iterator (next-entry hash-table)
(loop
(loop
(multiple-value-bind (nextp key value) (next-entry)
(multiple-value-bind (nextp key value) (next-entry)
(if (not nextp)
(if (not nextp)
(return)
(return)
(format t "~&Key: ~a, Value: ~a." key value)))))</lang>
(format t "~&Key: ~a, Value: ~a." key value)))))</syntaxhighlight>
===Alternate solution===
===Alternate solution===
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1]
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1]


<lang lisp>
<syntaxhighlight lang="lisp">
;; Project : Associative array/Iteration
;; Project : Associative array/Iteration


Line 702: Line 720:
(format t "~a" " : ")
(format t "~a" " : ")
(format t "~a" (aref x n 1)))
(format t "~a" (aref x n 1)))
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 709: Line 727:
! : 71
! : 71
</pre>
</pre>

=={{header|Crystal}}==
<syntaxhighlight lang="crystal">dict = {'A' => 1, 'B' => 2}

dict.each { |pair|
puts pair
}

dict.each_key { |key|
puts key
}

dict.each_value { |value|
puts value
}</syntaxhighlight>

{{out}}
<pre>{'A', 1}
{'B', 2}
A
B
1
2</pre>


=={{header|D}}==
=={{header|D}}==
{{works with|D|2}}
{{works with|D|2}}


<lang d>import std.stdio: writeln;
<syntaxhighlight lang="d">import std.stdio: writeln;


void main() {
void main() {
Line 752: Line 793:
foreach (value; aa.values)
foreach (value; aa.values)
writeln("7) Got value ", value);
writeln("7) Got value ", value);
}</lang>
}</syntaxhighlight>


=={{header|Dao}}==
=={{header|Dao}}==
<lang ruby>
<syntaxhighlight lang="ruby">
dict = { 'def' => 1, 'abc' => 2 }
dict = { 'def' => 1, 'abc' => 2 }


Line 763: Line 804:
io.writeln( key, value )
io.writeln( key, value )
}
}
</syntaxhighlight>
</lang>


=={{header|Dart}}==
=={{header|Dart}}==
<lang javascript>
<syntaxhighlight lang="javascript">
main(){
main(){
var fruits = {
var fruits = {
Line 785: Line 826:
fruits.values.forEach( ( value ) => print( value ) );
fruits.values.forEach( ( value ) => print( value ) );
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 811: Line 852:


=={{header|Delphi}}==
=={{header|Delphi}}==
<lang Delphi>program AssociativeArrayIteration;
<syntaxhighlight lang="delphi">program AssociativeArrayIteration;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 839: Line 880:
lDictionary.Free;
lDictionary.Free;
end;
end;
end.</lang>
end.</syntaxhighlight>


=={{header|Dyalect}}==
=={{header|Dyalect}}==


<lang dyalect>var t = (x = 1, y = 2, z = 3)
<syntaxhighlight lang="dyalect">var t = (x: 1, y: 2, z: 3)


for x in t.keys() {
for x in t.Keys() {
print("\(x)=\(t[x])")
print("\(x)=\(t[x])")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 861: Line 902:
The <code>for</code> loop takes either one pattern, for the value, or two, for the key and value; for iterating over keys alone the value may be given an ignore-pattern (<code>_</code>).
The <code>for</code> loop takes either one pattern, for the value, or two, for the key and value; for iterating over keys alone the value may be given an ignore-pattern (<code>_</code>).


<lang e>def map := [
<syntaxhighlight lang="e">def map := [
"a" => 1,
"a" => 1,
"b" => 2,
"b" => 2,
Line 881: Line 922:
for key in map.domain() { # iterate over the set whose values are the keys
for key in map.domain() { # iterate over the set whose values are the keys
println(`$key .`)
println(`$key .`)
}</lang>
}</syntaxhighlight>

=={{header|EasyLang}}==
<syntaxhighlight>
# use array of array for this
clothing$[][] = [ [ "type" "t-shirt" ] [ "color" "red" ] [ "size" "xl" ] ]
for i to len clothing$[][]
print clothing$[i][1] & ": " & clothing$[i][2]
.</syntaxhighlight>
{{out}}
<pre>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(lib 'hash) ;; load hash.lib
(lib 'hash) ;; load hash.lib
(define H (make-hash))
(define H (make-hash))
Line 909: Line 960:
value-> 666
value-> 666
value-> 33
value-> 33
</syntaxhighlight>
</lang>


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 5.0 :
ELENA 5.0 :
<lang elena>import system'collections;
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
import system'routines;
import extensions;
import extensions;
Line 930: Line 981:
map.forEach:
map.forEach:
(keyValue){ console.printLine(keyValue.Key," : ",keyValue.Value) }
(keyValue){ console.printLine(keyValue.Key," : ",keyValue.Value) }
}</lang>
}</syntaxhighlight>


=== Strong typed dictionary ===
=== Strong typed dictionary ===
<lang elena>import system'collections;
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
import system'routines;
import extensions;
import extensions;
Line 950: Line 1,001:
map.forEach:
map.forEach:
(tuple){ console.printLine(tuple.Item1," : ",tuple.Item2) }
(tuple){ console.printLine(tuple.Item1," : ",tuple.Item2) }
}</lang>
}</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>IO.inspect d = Map.new([foo: 1, bar: 2, baz: 3])
<syntaxhighlight lang="elixir">IO.inspect d = Map.new([foo: 1, bar: 2, baz: 3])
Enum.each(d, fn kv -> IO.inspect kv end)
Enum.each(d, fn kv -> IO.inspect kv end)
Enum.each(d, fn {k,v} -> IO.puts "#{inspect k} => #{v}" end)
Enum.each(d, fn {k,v} -> IO.puts "#{inspect k} => #{v}" end)
Enum.each(Map.keys(d), fn key -> IO.inspect key end)
Enum.each(Map.keys(d), fn key -> IO.inspect key end)
Enum.each(Map.values(d), fn value -> IO.inspect value end)</lang>
Enum.each(Map.values(d), fn value -> IO.inspect value end)</syntaxhighlight>


{{out}}
{{out}}
Line 974: Line 1,025:
3
3
1
1
</pre>

=={{header|EMal}}==
<syntaxhighlight lang="emal">
Map map = text%text["Italy" => "Rome", "France" => "Paris"]
map.insert("Germany", "Berlin")
map["Spain"] = "Madrid"
writeLine("== pairs ==")
for each Pair pair in map
writeLine(pair)
end
writeLine("== keys ==")
for each text key in map.keys()
writeLine(key)
end
writeLine("== values ==")
for each text value in map.values()
writeLine(value)
end
</syntaxhighlight>
{{out}}
<pre>
== pairs ==
[Italy,Rome]
[France,Paris]
[Germany,Berlin]
[Spain,Madrid]
== keys ==
Italy
France
Germany
Spain
== values ==
Rome
Paris
Berlin
Madrid
</pre>
</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>
<syntaxhighlight lang="erlang">
-module(assoc).
-module(assoc).
-compile([export_all]).
-compile([export_all]).
Line 991: Line 1,079:
io:format("~p: ~b~n",[K,dict:fetch(K,D)])
io:format("~p: ~b~n",[K,dict:fetch(K,D)])
end, dict:fetch_keys(D)).
end, dict:fetch_keys(D)).
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,001: Line 1,089:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Iterating over both.
Iterating over both.
<lang fsharp>
<syntaxhighlight lang="fsharp">
let myMap = [ ("Hello", 1); ("World", 2); ("!", 3) ]
let myMap = [ ("Hello", 1); ("World", 2); ("!", 3) ]


for k, v in myMap do
for k, v in myMap do
printfn "%s -> %d" k v
printfn "%s -> %d" k v
</syntaxhighlight>
</lang>


Iterating over either keys or values only can be achieved through use of the _ wildcard token.
Iterating over either keys or values only can be achieved through use of the _ wildcard token.
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Only prints the keys.
// Only prints the keys.
for k, _ in myMap do
for k, _ in myMap do
Line 1,017: Line 1,105:
for _, v in myMap do
for _, v in myMap do
printfn "%d" v
printfn "%d" v
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>H{ { "hi" "there" } { "a" "b" } } [ ": " glue print ] assoc-each</lang>
<syntaxhighlight lang="factor">H{ { "hi" "there" } { "a" "b" } } [ ": " glue print ] assoc-each</syntaxhighlight>
<code>assoc-each</code> places both the key and the value on top of the data stack. A simple <code>drop</code> or <code>nip</code> enables iterating over only keys or values.
<syntaxhighlight lang="factor">H{ { "hi" "there" } { "a" "b" } } [ drop print ] assoc-each ! print keys
H{ { "hi" "there" } { "a" "b" } } [ nip print ] assoc-each ! print values</syntaxhighlight>
There's also <code>assoc-map</code>, <code>assoc-find</code>, <code>assoc-filter</code> and many more.
There's also <code>assoc-map</code>, <code>assoc-find</code>, <code>assoc-filter</code> and many more.


Line 1,027: Line 1,118:
Given a map, <code>each</code> iterates over pairs of values-keys. <code>keys</code> and <code>vals</code> retrieve a list of keys or values, respectively.
Given a map, <code>each</code> iterates over pairs of values-keys. <code>keys</code> and <code>vals</code> retrieve a list of keys or values, respectively.


<lang fantom>
<syntaxhighlight lang="fantom">
class Main
class Main
{
{
Line 1,050: Line 1,141:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
{{libheader|Forth Foundation Library}}
{{libheader|Forth Foundation Library}}


<lang forth>include ffl/hct.fs
<syntaxhighlight lang="forth">include ffl/hct.fs
include ffl/hci.fs
include ffl/hci.fs


Line 1,076: Line 1,167:
;
;


iterate</lang>
iterate</syntaxhighlight>


<lang forth>
<syntaxhighlight lang="forth">
\ Written in ANS-Forth; tested under VFX.
\ Written in ANS-Forth; tested under VFX.
\ Requires the novice package: http://www.forth.org/novice.html
\ Requires the novice package: http://www.forth.org/novice.html
Line 1,216: Line 1,307:


some-languages kill-association
some-languages kill-association
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
Line 1,275: Line 1,366:
Ruby invented by: Matsumoto, Yukihiro
Ruby invented by: Matsumoto, Yukihiro
count: 10
count: 10
</pre>

=={{header|FreeBASIC}}==

Use the typedefs and data from [[Associative Array/Creation#FreeBASIC]] as an include.

Since this data structure stores the keys and values together it makes little sense to iterate through the same array three times to print different parts of it, hence I will only print the key:value pairs.

<syntaxhighlight lang="freebasic">#include"assoc.bas"

function get_dict_data_string( d as dicitem ) as string
select case d.datatype
case BOOL
if d.value.bool then return "true" else return "false"
case INTEG
return str(d.value.integ)
case STRNG
return """"+d.value.strng+""""
case FLOAT
return str(d.value.float)
case BYYTE
return str(d.value.byyte)
case else
return "DATATYPE ERROR"
end select
end function

sub print_keyval_pair( d as dicentry )
print using "{&} : {&}";get_dict_data_string( d.key ); get_dict_data_string(d.value)
end sub

for i as uinteger = 0 to ubound(Dictionary)
print_keyval_pair(Dictionary(i))
next i</syntaxhighlight>

{{out}}
<pre>
{"Cat"} : {"Mittens"}
{32767} : {2.718281828}
</pre>
</pre>


=={{header|Free Pascal}}==
=={{header|Free Pascal}}==
FPC 3.2.0+. Similar to Delphi:<lang pascal>program AssociativeArrayIteration;
FPC 3.2.0+. Similar to Delphi:<syntaxhighlight lang="pascal">program AssociativeArrayIteration;
{$mode delphi}{$ifdef windows}{$apptype console}{$endif}
{$mode delphi}{$ifdef windows}{$apptype console}{$endif}
uses Generics.Collections;
uses Generics.Collections;
Line 1,307: Line 1,438:
lDictionary.Free;
lDictionary.Free;
end;
end;
end.</lang>
end.</syntaxhighlight>
<pre>
<pre>
Pair: foo = 6
Pair: foo = 6
Line 1,320: Line 1,451:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>d = new dict[[[1, "one"], [2, "two"]]]
<syntaxhighlight lang="frink">d = new dict[[[1, "one"], [2, "two"]]]
for [key, value] = d
for [key, value] = d
println["$key\t$value"]
println["$key\t$value"]
Line 1,327: Line 1,458:
for key = keys[d]
for key = keys[d]
println["$key"]
println["$key"]
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,337: Line 1,468:
1
1
</pre>
</pre>

=={{header|FutureBasic}}==
There are many ways to iterate over an associative array (dictionary) in FutureBasic. Below are a selection.

1. for ... in ...
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
CFStringRef key
for key in dict
print key, dict[key]
next
end fn

fn DoIt

HandleEvents
</syntaxhighlight>

2. Enumerator callback
<syntaxhighlight lang="futurebasic">
void local fn MyDictEnumerator( dict as CFDictionaryRef, key as CFTypeRef, obj as CFTypeRef, stp as ^BOOL, userData as ptr )
print key, obj
end fn

void local fn DoIt
CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
DictionaryEnumerateKeysAndObjects( dict, @fn MyDictEnumerator, NULL )
end fn

fn DoIt

HandleEvents
</syntaxhighlight>

3. Array of keys
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
CFArrayRef keys = fn DictionaryAllKeys( dict )
CFStringRef key
for key in keys
print key, dict[key]
next
end fn

fn DoIt

HandleEvents
</syntaxhighlight>

4. Array of values
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
CFArrayRef values = fn DictionaryAllValues( dict )
CFStringRef value
for value in values
print value
next
end fn

fn DoIt

HandleEvents
</syntaxhighlight>

5. Key/object enumerators
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
CFStringRef key
CFTypeRef obj
EnumeratorRef keyEnumerator = fn DictionaryKeyEnumerator( dict )
key = fn EnumeratorNextObject( keyEnumerator )
while ( key )
print key,dict[key]
key = fn EnumeratorNextObject( keyEnumerator )
wend
print
EnumeratorRef objectEnumerator = fn DictionaryObjectEnumerator( dict )
obj = fn EnumeratorNextObject( objectEnumerator )
while ( obj )
print obj
obj = fn EnumeratorNextObject( objectEnumerator )
wend
end fn

fn DoIt

HandleEvents
</syntaxhighlight>


=={{header|Gambas}}==
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=e48bd6ed7e6b583106b8178bca536eea Click this link to run this code]'''
'''[https://gambas-playground.proko.eu/?gist=e48bd6ed7e6b583106b8178bca536eea Click this link to run this code]'''
<lang gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()
Dim cList As Collection = ["2": "quick", "4": "fox", "1": "The", "9": "dog", "7": "the", "5": "jumped", "3": "brown", "6": "over", "8": "lazy"]
Dim cList As Collection = ["2": "quick", "4": "fox", "1": "The", "9": "dog", "7": "the", "5": "jumped", "3": "brown", "6": "over", "8": "lazy"]
Dim siCount As Short
Dim siCount As Short
Line 1,355: Line 1,584:
Next
Next


End</lang>
End</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,364: Line 1,593:
=={{header|Go}}==
=={{header|Go}}==
'''Language:'''
'''Language:'''
<lang go>myMap := map[string]int {
<syntaxhighlight lang="go">myMap := map[string]int {
"hello": 13,
"hello": 13,
"world": 31,
"world": 31,
Line 1,382: Line 1,611:
for _, value := range myMap {
for _, value := range myMap {
fmt.Printf("value = %d\n", value)
fmt.Printf("value = %d\n", value)
}</lang>
}</syntaxhighlight>
'''Standard library templates:'''
'''Standard library templates:'''


Line 1,390: Line 1,619:
* In a template, if map keys are a comparable basic type, then iteration proceeds in key order. With the language for/range, iteration is in non-deterministic order.
* In a template, if map keys are a comparable basic type, then iteration proceeds in key order. With the language for/range, iteration is in non-deterministic order.


<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,424: Line 1,653:
{{end -}}
{{end -}}
`)).Execute(os.Stdout, m)
`)).Execute(os.Stdout, m)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Note order by key.
Note order by key.
Line 1,441: Line 1,670:
=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
<lang groovy>def map = [lastName: "Anderson", firstName: "Thomas", nickname: "Neo", age: 24, address: "everywhere"]
<syntaxhighlight lang="groovy">def map = [lastName: "Anderson", firstName: "Thomas", nickname: "Neo", age: 24, address: "everywhere"]


println "Entries:"
println "Entries:"
Line 1,452: Line 1,681:
println()
println()
println "Values:"
println "Values:"
map.values().each { println it }</lang>
map.values().each { println it }</syntaxhighlight>


{{out}}
{{out}}
Line 1,477: Line 1,706:


=={{header|Harbour}}==
=={{header|Harbour}}==
<lang visualfoxpro>LOCAL arr := { 6 => 16, "eight" => 8, "eleven" => 11 }
<syntaxhighlight lang="visualfoxpro">LOCAL arr := { 6 => 16, "eight" => 8, "eleven" => 11 }
LOCAL x
LOCAL x


Line 1,487: Line 1,716:
// or value only
// or value only
? x
? x
NEXT</lang>
NEXT</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
with Data.Map:
with Data.Map:
<lang haskell>import qualified Data.Map as M
<syntaxhighlight lang="haskell">import qualified Data.Map as M


myMap :: M.Map String Int
myMap :: M.Map String Int
Line 1,503: Line 1,732:
, show . M.elems -- Values
, show . M.elems -- Values
] <*>
] <*>
pure myMap</lang>
pure myMap</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[("!",71),("hello",13),("world",31)]
<pre>[("!",71),("hello",13),("world",31)]
Line 1,510: Line 1,739:


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang icon>procedure main()
<syntaxhighlight lang="icon">procedure main()
t := table()
t := table()
every t[a := !"ABCDE"] := map(a)
every t[a := !"ABCDE"] := map(a)
Line 1,524: Line 1,753:
every writes(" ",!t)
every writes(" ",!t)
write()
write()
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,537: Line 1,766:


=={{header|Io}}==
=={{header|Io}}==
<lang Io>myDict := Map with(
<syntaxhighlight lang="io">myDict := Map with(
"hello", 13,
"hello", 13,
"world", 31,
"world", 31,
Line 1,560: Line 1,789:
myDict values foreach( value,
myDict values foreach( value,
writeln("value = ", value)
writeln("value = ", value)
)</lang>
)</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 1,568: Line 1,797:
Using the J example from [[Creating an Associative Array]]...
Using the J example from [[Creating an Associative Array]]...


Keys <lang J>nl__example 0</lang>
Keys <syntaxhighlight lang="j">nl__example 0</syntaxhighlight>


Values <lang J>get__example each nl__example 0</lang>
Values <syntaxhighlight lang="j">get__example each nl__example 0</syntaxhighlight>


Both keys and values <lang J>(,&< get__example) each nl__example 0</lang>
Both keys and values <syntaxhighlight lang="j">(,&< get__example) each nl__example 0</syntaxhighlight>


Note that this last is not likely to be useful in any practical context outside of learning the language.
Note that this last is not likely to be useful in any practical context outside of learning the language.


=={{header|Java}}==
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
<lang java>Map<String, Integer> map = new HashMap<String, Integer>();
fn main() {
map.put("hello", 1);
let dictionary = ["foo": 1, "bar": 2]
map.put("world", 2);
for entry in dictionary {
map.put("!", 3);
// To get values, use
// let value = entry.1
println("{}", entry)
}


// Just keys
// iterating over key-value pairs:
for key in dictionary.keys() {
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
println("{}", key)
}
Integer value = e.getValue();
System.out.println("key = " + key + ", value = " + value);
}
}
</syntaxhighlight>
{{out}}
<pre>
("bar", 2)
("foo", 1)
bar
foo
</pre>


=={{header|Java}}==
// iterating over keys:
<p>
for (String key : map.keySet()) {
See also, [https://rosettacode.org/wiki/Associative_array/Creation#Java Java - Associative array/Creation].
System.out.println("key = " + key);
</p>
}
<p>

You can access the <kbd>key</kbd> and <kbd>value</kbd> pairs by using the <code>Map.entrySet</code> method,
// iterating over values:
which will return a <code>Map.Entry</code>.<br />
for (Integer value : map.values()) {
It's worth noting that a <code>Map.Entry</code> also has the <code>setValue</code> method.
System.out.println("value = " + value);
}</lang>
</p>
<syntaxhighlight lang="java">
for (Map.Entry<String, Integer> entry : map.entrySet())
System.out.println(entry);
</syntaxhighlight>
<p>
You can access just the <kbd>key</kbd>s by using the <code>Map.keySet</code> method, which will return a <code>Set</code>.
</p>
<syntaxhighlight lang="java">
for (String key : map.keySet())
System.out.println(key);
</syntaxhighlight>
<p>
And you can access just the <kbd>value</kbd>s by using the <code>Map.values</code> method, which will return a <code>Collection</code>.
</p>
<syntaxhighlight lang="java">
for (int value : map.values())
System.out.println(value);
</syntaxhighlight>
<br />


Java 8 version
Java 8 version


<lang java>Map<String, Integer> map = new HashMap<>();
<syntaxhighlight lang="java">Map<String, Integer> map = new HashMap<>();
map.put("hello", 1);
map.put("hello", 1);
map.put("world", 2);
map.put("world", 2);
Line 1,615: Line 1,874:


// iterating over values:
// iterating over values:
map.values().forEach(v -> System.out.printf("value = %s%n", v));</lang>
map.values().forEach(v -> System.out.printf("value = %s%n", v));</syntaxhighlight>


{{out}}
{{out}}
Line 1,630: Line 1,889:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
JavaScript does not have associative arrays until ECMAScript 6 brings Maps. In versions up to ES5.1, you may add properties to an empty object to achieve the same effect.
JavaScript does not have associative arrays until ECMAScript 6 brings Maps. In versions up to ES5.1, you may add properties to an empty object to achieve the same effect.
<lang javascript>var myhash = {}; //a new, empty object
<syntaxhighlight lang="javascript">var myhash = {}; //a new, empty object
myhash["hello"] = 3;
myhash["hello"] = 3;
myhash.world = 6; //obj.name is equivalent to obj["name"] for certain values of name
myhash.world = 6; //obj.name is equivalent to obj["name"] for certain values of name
Line 1,647: Line 1,906:
keys.map(function (key) {
keys.map(function (key) {
console.log("Key is: " + key + '. Value is: ' + myhash[key]);
console.log("Key is: " + key + '. Value is: ' + myhash[key]);
});</lang>
});</syntaxhighlight>


=={{header|Jq}}==
=={{header|Jq}}==
Line 1,662: Line 1,921:


In jq > 1.4, keys_unsorted, for producing an array of the keys (in the order of creation), is also available.
In jq > 1.4, keys_unsorted, for producing an array of the keys (in the order of creation), is also available.
<lang jq>def mydict: {"hello":13, "world": 31, "!": 71};
<syntaxhighlight lang="jq">def mydict: {"hello":13, "world": 31, "!": 71};


# Iterating over the keys
# Iterating over the keys
Line 1,693: Line 1,952:
# ["world",31]
# ["world",31]
# ["!",71]
# ["!",71]
</syntaxhighlight>
</lang>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}
<lang julia>dict = Dict("hello" => 13, "world" => 31, "!" => 71)
<syntaxhighlight lang="julia">dict = Dict("hello" => 13, "world" => 31, "!" => 71)


# applying a function to key-value pairs:
# applying a function to key-value pairs:
Line 1,716: Line 1,975:
@show value
@show value
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,733: Line 1,992:
=={{header|K}}==
=={{header|K}}==
Creating a dictionary.
Creating a dictionary.
<lang K> d: .((`"hello";1); (`"world";2);(`"!";3))</lang>
<syntaxhighlight lang="k"> d: .((`"hello";1); (`"world";2);(`"!";3))</syntaxhighlight>


The keys are available via "!".
The keys are available via "!".
<lang K> !d
<syntaxhighlight lang="k"> !d
`hello `world `"!"
`hello `world `"!"


Line 1,742: Line 2,001:
("hello"
("hello"
"world"
"world"
,"!")</lang>
,"!")</syntaxhighlight>


Print the key value pairs.
Print the key value pairs.
<lang K> `0:{,/$x,": ",d[x]}'!d
<syntaxhighlight lang="k"> `0:{,/$x,": ",d[x]}'!d
hello: 1
hello: 1
world: 2
world: 2
!: 3</lang>
!: 3</syntaxhighlight>


The values are available via "[]".
The values are available via "[]".
<lang K> d[]
<syntaxhighlight lang="k"> d[]
1 2 3
1 2 3


{x+1}'d[]
{x+1}'d[]
2 3 4</lang>
2 3 4</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>fun main(a: Array<String>) {
<syntaxhighlight lang="scala">fun main() {
val map = mapOf("hello" to 1, "world" to 2, "!" to 3)
val map = mapOf("hello" to 1, "world" to 2, "!" to 3)


with(map) {
with(map) {
entries.forEach { println("key = ${it.key}, value = ${it.value}") }
forEach { println("key = ${it.key}, value = ${it.value}") }
keys.forEach { println("key = $it") }
keys.forEach { println("key = $it") }
values.forEach { println("value = $it") }
values.forEach { println("value = $it") }
}
}
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>key = hello, value = 1
<pre>key = hello, value = 1
Line 1,779: Line 2,038:


=={{header|Lang5}}==
=={{header|Lang5}}==
<lang lang5>: first 0 extract nip ; : second 1 extract nip ; : nip swap drop ;
<syntaxhighlight lang="lang5">: first 0 extract nip ; : second 1 extract nip ; : nip swap drop ;
: say(*) dup first " => " 2 compress "" join . second . ;
: say(*) dup first " => " 2 compress "" join . second . ;


[['foo 5] ['bar 10] ['baz 20]] 'say apply drop</lang>
[['foo 5] ['bar 10] ['baz 20]] 'say apply drop</syntaxhighlight>


=={{header|Lasso}}==
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">
<lang Lasso>
//iterate over associative array
//iterate over associative array
//Lasso maps
//Lasso maps
Line 1,815: Line 2,074:
//the {^ ^} indicates that output should be printed (AutoCollect) ,
//the {^ ^} indicates that output should be printed (AutoCollect) ,
// if output is not desired, just { } is used
// if output is not desired, just { } is used
</syntaxhighlight>
</lang>


=={{header|LFE}}==
=={{header|LFE}}==


===Keys and Values===
===Keys and Values===
<lang lisp>
<syntaxhighlight lang="lisp">
(let ((data '(#(key1 "foo") #(key2 "bar")))
(let ((data '(#(key1 "foo") #(key2 "bar")))
(hash (: dict from_list data)))
(hash (: dict from_list data)))
Line 1,828: Line 2,087:
0
0
hash))
hash))
</syntaxhighlight>
</lang>


===Just Keys===
===Just Keys===
<lang lisp>
<syntaxhighlight lang="lisp">
(let ((data '(#(key1 "foo") #(key2 "bar")))
(let ((data '(#(key1 "foo") #(key2 "bar")))
(hash (: dict from_list data)))
(hash (: dict from_list data)))
Line 1,838: Line 2,097:
(: io format '"~s~n" (list key)))
(: io format '"~s~n" (list key)))
(: dict fetch_keys hash)))
(: dict fetch_keys hash)))
</syntaxhighlight>
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
Needs the sublist library from http://basic.wikispaces.com/SubList+Library since LB does not have built-in associative arrays.
Needs the sublist library from http://basic.wikispaces.com/SubList+Library since LB does not have built-in associative arrays.
<syntaxhighlight lang="lb">
<lang lb>
data "red", "255 50 50", "green", "50 255 50", "blue", "50 50 255"
data "red", "255 50 50", "green", "50 255 50", "blue", "50 50 255"
data "my fave", "220 120 120", "black", "0 0 0"
data "my fave", "220 120 120", "black", "0 0 0"
Line 1,866: Line 2,125:


end
end
</syntaxhighlight>
</lang>
Number of key-data pairs =5
Number of key-data pairs =5
Key 1: red Data: 255 50 50
Key 1: red Data: 255 50 50
Line 1,875: Line 2,134:


=={{header|Lingo}}==
=={{header|Lingo}}==
<lang lingo>hash = [#key1:"value1", #key2:"value2", #key3:"value3"]
<syntaxhighlight lang="lingo">hash = [#key1:"value1", #key2:"value2", #key3:"value3"]


-- iterate over key-value pairs
-- iterate over key-value pairs
Line 1,885: Line 2,144:
repeat with val in hash
repeat with val in hash
put val
put val
end repeat</lang>
end repeat</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>put 3 into fruit["apples"]
<syntaxhighlight lang="livecode">put 3 into fruit["apples"]
put 5 into fruit["pears"]
put 5 into fruit["pears"]
put 6 into fruit["oranges"]
put 6 into fruit["oranges"]
Line 1,911: Line 2,170:
-- alternatively, use same loop as for values 1 with tkey && fruit[tKey]
-- alternatively, use same loop as for values 1 with tkey && fruit[tKey]


put tTmp</lang>
put tTmp</syntaxhighlight>
Output
Output
<lang LiveCode>Keys:
<syntaxhighlight lang="livecode">Keys:
apples
apples
pears
pears
Line 1,924: Line 2,183:
bananas:none
bananas:none
oranges:6
oranges:6
pears:5</lang>
pears:5</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>local t = {
<syntaxhighlight lang="lua">local t = {
["foo"] = "bar",
["foo"] = "bar",
["baz"] = 6,
["baz"] = 6,
Line 1,935: Line 2,194:
for key,val in pairs(t) do
for key,val in pairs(t) do
print(string.format("%s: %s", key, val))
print(string.format("%s: %s", key, val))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,946: Line 2,205:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module checkit {
Module checkit {
\\ Inventories are objects with keys and values, or keys (used as read only values)
\\ Inventories are objects with keys and values, or keys (used as read only values)
Line 2,004: Line 2,263:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


=={{header|M4}}==
=={{header|M4}}==
<lang M4>divert(-1)
<syntaxhighlight lang="m4">divert(-1)
define(`for',
define(`for',
`ifelse($#,0,``$0'',
`ifelse($#,0,``$0'',
Line 2,041: Line 2,300:
for(`x',1,asize(`a'),
for(`x',1,asize(`a'),
`avalue(`a',x)
`avalue(`a',x)
')</lang>
')</syntaxhighlight>


{{out}}
{{out}}
Line 2,060: Line 2,319:
=={{header|Maple}}==
=={{header|Maple}}==
Iterate through indices when indices are all simple expressions:
Iterate through indices when indices are all simple expressions:
<syntaxhighlight lang="maple">
<lang Maple>
> T := table( [ "A" = 1, "B" = 2, "C" = 3, "D" = 4 ] );
> T := table( [ "A" = 1, "B" = 2, "C" = 3, "D" = 4 ] );
> for i in indices( T, nolist ) do print(i ) end:
> for i in indices( T, nolist ) do print(i ) end:
Line 2,070: Line 2,329:


"D"
"D"
</syntaxhighlight>
</lang>


Iterate through indices when indices may be expression sequences:
Iterate through indices when indices may be expression sequences:
<syntaxhighlight lang="maple">
<lang Maple>
> T := table( [ "a" = 1, "b" = 2, ("c","d") = 3 ] ):
> T := table( [ "a" = 1, "b" = 2, ("c","d") = 3 ] ):
> for i in indices( T ) do print( i, T[ op( i ) ] ) end:
> for i in indices( T ) do print( i, T[ op( i ) ] ) end:
Line 2,081: Line 2,340:


["c", "d"], 3
["c", "d"], 3
</syntaxhighlight>
</lang>


Return all index / entry pairs as equations:
Return all index / entry pairs as equations:
<syntaxhighlight lang="maple">
<lang Maple>
> for i in indices( T, pairs ) do print( i) end:
> for i in indices( T, pairs ) do print( i) end:
"a" = 1
"a" = 1
Line 2,091: Line 2,350:


("c", "d") = 3
("c", "d") = 3
</syntaxhighlight>
</lang>


<syntaxhighlight lang="maple">
<lang Maple>
> for i in entries( T ) do print( i) end:
> for i in entries( T ) do print( i) end:
[1]
[1]
Line 2,100: Line 2,359:


[2]
[2]
</syntaxhighlight>
</lang>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>keys=DownValues[#,Sort->False][[All,1,1,1]]&;
<syntaxhighlight lang="mathematica">keys=DownValues[#,Sort->False][[All,1,1,1]]&;
hashes=#/@keys[#]&;
hashes=#/@keys[#]&;


Line 2,110: Line 2,369:
->{2,sometext}
->{2,sometext}
hashes[a]
hashes[a]
->{string,23}</lang>
->{string,23}</syntaxhighlight>


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
Line 2,116: Line 2,375:
Associative arrays can be defined as structs in Matlab and Octave.
Associative arrays can be defined as structs in Matlab and Octave.


<lang Matlab> keys = fieldnames(hash);
<syntaxhighlight lang="matlab"> keys = fieldnames(hash);
for k=1:length(keys),
for k=1:length(keys),
key = keys{k};
key = keys{k};
value = getfield(hash,key); % get value of key
value = getfield(hash,key); % get value of key
hash = setfield(hash,key,-value); % set value of key
hash = setfield(hash,key,-value); % set value of key
end; </lang>
end; </syntaxhighlight>


or
or


<lang Matlab> keys = fieldnames(hash);
<syntaxhighlight lang="matlab"> keys = fieldnames(hash);
for k=1:length(keys),
for k=1:length(keys),
key = keys{k};
key = keys{k};
value = hash.(key); % get value of key
value = hash.(key); % get value of key
hash.(key) = -value; % set value of key
hash.(key) = -value; % set value of key
end; </lang>
end; </syntaxhighlight>


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang Maxima>h[1]: 6$
<syntaxhighlight lang="maxima">h[1]: 6$
h[9]: 2$
h[9]: 2$


Line 2,143: Line 2,402:
for key in rest(arrayinfo(h), 2) do (
for key in rest(arrayinfo(h), 2) do (
val: arrayapply(h, key),
val: arrayapply(h, key),
print(key, val))$</lang>
print(key, val))$</syntaxhighlight>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>d = { 3: "test", "foo": 3 }
<syntaxhighlight lang="miniscript">d = { 3: "test", "foo": 3 }


for keyVal in d
for keyVal in d
Line 2,158: Line 2,417:
for val in d.values
for val in d.values
print val
print val
end for</lang>
end for</syntaxhighlight>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols
options replace format comments java crossref symbols


Line 2,174: Line 2,433:
loop fn over surname
loop fn over surname
say fn.right(10) ':' surname[fn]
say fn.right(10) ':' surname[fn]
end fn</lang>
end fn</syntaxhighlight>


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>;; using an association list:
<syntaxhighlight lang="newlisp">;; using an association list:
(setq alist '(("A" "a") ("B" "b") ("C" "c")))
(setq alist '(("A" "a") ("B" "b") ("C" "c")))


Line 2,188: Line 2,447:
;; loop over the assocation list:
;; loop over the assocation list:
(dolist (elem alist)
(dolist (elem alist)
(println (format "%s -> %s" (first elem) (last elem))))</lang>
(println (format "%s -> %s" (first elem) (last elem))))</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>
<syntaxhighlight lang="nim">
import tables
import tables


var t: Table[int,string] = initTable[int,string]()
var t: Table[int,string]


t[1] = "one"
t[1] = "one"
t[2] = "two"
t[2] = "two"
t[3] = "three"
t[3] = "three"
t.add(4,"four")
t[4] = "four"


echo "t has " & $t.len & " elements"
echo "t has " & $t.len & " elements"
Line 2,211: Line 2,470:
echo "at[" & $k & "]=" & t[k]
echo "at[" & $k & "]=" & t[k]


#itetate pairs
#iterate pairs
echo "pair iteration:"
echo "pair iteration:"
for k,v in t.pairs:
for k,v in t.pairs:
echo "at[" & $k & "]=" & v
echo "at[" & $k & "]=" & v
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,235: Line 2,494:
=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
{{works with|oo2c Version 2}}
{{works with|oo2c Version 2}}
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE AssociativeArray;
MODULE AssociativeArray;
IMPORT
IMPORT
Line 2,277: Line 2,536:
END AssociativeArray.
END AssociativeArray.
</syntaxhighlight>
</lang>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<syntaxhighlight lang="objeck">
class Iteration {
class Iteration {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
Line 2,306: Line 2,565:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
{{works with|Objective-C|2.0+}}
{{works with|Objective-C|2.0+}}
<lang objc>NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
<syntaxhighlight lang="objc">NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:31], @"world",
[NSNumber numberWithInt:31], @"world",
Line 2,323: Line 2,582:
for (id value in [myDict objectEnumerator]) {
for (id value in [myDict objectEnumerator]) {
NSLog(@"value = %@", value);
NSLog(@"value = %@", value);
}</lang>
}</syntaxhighlight>


{{works with|Objective-C|<2.0}}
{{works with|Objective-C|<2.0}}
<lang objc>NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
<syntaxhighlight lang="objc">NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:31], @"world",
[NSNumber numberWithInt:31], @"world",
Line 2,343: Line 2,602:
while ((value = [enm nextObject])) {
while ((value = [enm nextObject])) {
NSLog(@"value = %@", value);
NSLog(@"value = %@", value);
}</lang>
}</syntaxhighlight>


{{works with|Cocoa|Mac OS X 10.6+}}
{{works with|Cocoa|Mac OS X 10.6+}}
<lang objc>NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
<syntaxhighlight lang="objc">NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:31], @"world",
[NSNumber numberWithInt:31], @"world",
Line 2,354: Line 2,613:
[myDict enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
[myDict enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
NSLog(@"key = %@, value = %@", key, value);
NSLog(@"key = %@, value = %@", key, value);
}];</lang>
}];</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Association array:
Association array:
<lang ocaml>#!/usr/bin/env ocaml
<syntaxhighlight lang="ocaml">#!/usr/bin/env ocaml


let map = [| ('A', 1); ('B', 2); ('C', 3) |] ;;
let map = [| ('A', 1); ('B', 2); ('C', 3) |] ;;
Line 2,372: Line 2,631:


(* in functional programming it is often more useful to fold over the elements *)
(* in functional programming it is often more useful to fold over the elements *)
Array.fold_left (fun acc (k,v) -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) "Elements:\n" map ;;</lang>
Array.fold_left (fun acc (k,v) -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) "Elements:\n" map ;;</syntaxhighlight>


Hash table:
Hash table:
<lang ocaml>let map = Hashtbl.create 42;;
<syntaxhighlight lang="ocaml">let map = Hashtbl.create 42;;
Hashtbl.add map 'A' 1;;
Hashtbl.add map 'A' 1;;
Hashtbl.add map 'B' 2;;
Hashtbl.add map 'B' 2;;
Line 2,384: Line 2,643:


(* in functional programming it is often more useful to fold over the elements *)
(* in functional programming it is often more useful to fold over the elements *)
Hashtbl.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;</lang>
Hashtbl.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;</syntaxhighlight>


Functional binary search tree:
Functional binary search tree:
<lang ocaml>module CharMap = Map.Make (Char);;
<syntaxhighlight lang="ocaml">module CharMap = Map.Make (Char);;
let map = CharMap.empty;;
let map = CharMap.empty;;
let map = CharMap.add 'A' 1 map;;
let map = CharMap.add 'A' 1 map;;
Line 2,397: Line 2,656:


(* in functional programming it is often more useful to fold over the elements *)
(* in functional programming it is often more useful to fold over the elements *)
CharMap.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;</lang>
CharMap.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;</syntaxhighlight>


=={{header|Ol}}==
=={{header|Ol}}==
<syntaxhighlight lang="ol">
<lang ol>
;;; create sample associative array
;;; create sample associative array
(define aa (list->ff '(
(define aa (list->ff '(
Line 2,460: Line 2,719:
; ==> #((! . 9) (hello . 1) (world . 4))
; ==> #((! . 9) (hello . 1) (world . 4))


</syntaxhighlight>
</lang>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang oorexx>d = .directory~new
<syntaxhighlight lang="oorexx">d = .directory~new
d["hello"] = 1
d["hello"] = 1
d["world"] = 2
d["world"] = 2
Line 2,483: Line 2,742:
say "key =" s~index", value =" s~item
say "key =" s~index", value =" s~item
s~next
s~next
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>key = !
<pre>key = !
Line 2,496: Line 2,755:


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>declare
<syntaxhighlight lang="oz">declare
MyMap = unit('hello':13 'world':31 '!':71)
MyMap = unit('hello':13 'world':31 '!':71)
in
in
{ForAll {Record.toListInd MyMap} Show} %% pairs
{ForAll {Record.toListInd MyMap} Show} %% pairs
{ForAll {Record.arity MyMap} Show} %% keys
{ForAll {Record.arity MyMap} Show} %% keys
{ForAll {Record.toList MyMap} Show} %% values</lang>
{ForAll {Record.toList MyMap} Show} %% values</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Line 2,507: Line 2,766:


The keys can be retried from a map with Vec:
The keys can be retried from a map with Vec:
<lang parigp>keys = Vec(M);</lang>
<syntaxhighlight lang="parigp">keys = Vec(M);</syntaxhighlight>
You can iterate over the values as usual:
You can iterate over the values as usual:
<lang parigp>for(i=1,#keys,
<syntaxhighlight lang="parigp">for(i=1,#keys,
print(keys[i]," ",mapget(M,keys[i]))
print(keys[i]," ",mapget(M,keys[i]))
)</lang>
)</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#! /usr/bin/perl
<syntaxhighlight lang="perl">#! /usr/bin/perl
use strict;
use strict;


Line 2,545: Line 2,804:
foreach my $val ( values %pairs ) {
foreach my $val ( values %pairs ) {
print "value = $val\n";
print "value = $val\n";
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
The first three lines create a simple dictionary, with keys and values of several different types (string/integer/sequence):
The first three lines create a simple dictionary, with keys and values of several different types (string/integer/sequence):
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>setd("one",1)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
setd(2,"duo")
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
setd({3,4},{5,"six"})
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"duo"</span><span style="color: #0000FF;">)</span>

<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">({</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"six"</span><span style="color: #0000FF;">})</span>
function visitor(object key, object data, object /*userdata*/)
?{key,data}
<span style="color: #008080;">function</span> <span style="color: #000000;">visitor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000080;font-style:italic;">/*userdata*/</span><span style="color: #0000FF;">)</span>
return 1 -- (continue traversal)
<span style="color: #0000FF;">?{</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">}</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (continue traversal)</span>
traverse_dict(routine_id("visitor"))</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"visitor"</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,564: Line 2,826:
{"one",1}
{"one",1}
</pre>
</pre>
You could also use some of the map.e routines. With the same initial three setd() as above:
You could also use some of the map.e routines:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include builtins\map.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
?pairs()
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
?keys()
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"duo"</span><span style="color: #0000FF;">)</span>
?values()</lang>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">({</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"six"</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (map.e incompatible with p2js before that)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">map</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">pairs</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">keys</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">values</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,577: Line 2,847:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


def getd /# dict key -- dict data #/
( ( ) ( ) ) var dictionary
swap 1 get rot find nip

dup if
/#
swap 2 get rot get nip
def setd
else
1 get dictionary 1 get nip swap 0 put dictionary swap 1 set var dictionary
drop "Unfound"
2 get dictionary 2 get nip swap 0 put dictionary swap 2 set var dictionary
endif
enddef
enddef
#/



def setd /# ( key data ) -- #/
def setd /# dict ( key data ) -- dict #/
2 for
var i
1 get var ikey
i get
2 get var idata
dictionary i get nip
swap 0 put
dictionary swap i set var dictionary
endfor
drop
drop
1 get ikey find var p drop
enddef
p if

2 get idata p set 2 set
def getd /# key -- data #/
dictionary 1 get nip swap find nip
dup if
dictionary 2 get nip swap get nip
else
else
drop "Unfound"
2 get idata 0 put 2 set
1 get ikey 0 put 1 set
endif
endif
enddef
enddef



def pair /# n -- ( k d ) #/
def pair /# dict n -- dict ( k d ) #/
1 over 2 tolist var ikey
1 over 2 tolist var ikey
2 swap 2 tolist var idata
2 swap 2 tolist var idata
dictionary ikey sget
ikey sget
swap idata sget nip
swap idata sget rot swap
2 tolist
2 tolist
enddef
enddef


def scandict /# n -- #/
def scandict /# dict n -- dict ( ) #/
var n
dictionary 1 get len nip nip
1 get len nip
for
for
pair
pair
over 0 != if over get nip endif
n if n get nip endif
print nl
print nl
endfor
endfor
drop
enddef
enddef


def pairs
def pairs /# dict -- dict ( ) #/
0 scandict
0 scandict
enddef
enddef
Line 2,639: Line 2,904:


/# ---------- MAIN ---------- #/
/# ---------- MAIN ---------- #/

( ( ) ( ) )


( "one" 1 ) setd
( "one" 1 ) setd
Line 2,646: Line 2,913:
pairs nl
pairs nl
keys nl
keys nl
values</lang>
values
</syntaxhighlight>


=={{header|PHP}}==
=={{header|PHP}}==
<lang php><?php
<syntaxhighlight lang="php"><?php
$pairs = array( "hello" => 1,
$pairs = array( "hello" => 1,
"world" => 2,
"world" => 2,
Line 2,668: Line 2,936:
echo "values = $value\n";
echo "values = $value\n";
}
}
?></lang>
?></syntaxhighlight>

=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
Map = new_map([1=one,2=two,3=three,4=four]),
foreach(K=V in Map)
println(K=V)
end,
nl,

println(keys=Map.keys),
foreach(K in Map.keys.sort)
println(K=Map.get(K))
end,
nl,

println(values=Map.values),
foreach(V in Map.values.sort)
% This works but gets a warning: nonlocal_var_in_iterator_pattern
% println(V=[K : K=V in Map])
% No warning:
println(V=[K : K=V1 in Map,V1 == V])
end,
nl.</syntaxhighlight>

{{out}}
<pre>1 = one
2 = two
3 = three
4 = four

keys = [1,2,3,4]
1 = one
2 = two
3 = three
4 = four

values = [one,two,three,four]
four = [4]
one = [1]
three = [3]
two = [2]</pre>



=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
===Using properties===
===Using properties===
<lang PicoLisp>(put 'A 'foo 5)
<syntaxhighlight lang="picolisp">(put 'A 'foo 5)
(put 'A 'bar 10)
(put 'A 'bar 10)
(put 'A 'baz 15)
(put 'A 'baz 15)
Line 2,683: Line 2,994:


: (mapcar car (getl 'A)) # Get all values
: (mapcar car (getl 'A)) # Get all values
-> (15 10 5)</lang>
-> (15 10 5)</syntaxhighlight>
===Using an index tree===
===Using an index tree===
<lang PicoLisp>(idx 'A (def "foo" 5) T)
<syntaxhighlight lang="picolisp">(idx 'A (def "foo" 5) T)
(idx 'A (def "bar" 10) T)
(idx 'A (def "bar" 10) T)
(idx 'A (def "baz" 15) T)
(idx 'A (def "baz" 15) T)
Line 2,696: Line 3,007:


: (mapcar val (idx 'A)) # Get all values
: (mapcar val (idx 'A)) # Get all values
-> (10 15 5)</lang>
-> (10 15 5)</syntaxhighlight>


=={{header|Pike}}==
=={{header|Pike}}==
Line 2,702: Line 3,013:
the order is deterministic however.
the order is deterministic however.


<syntaxhighlight lang="pike">
<lang Pike>
mapping(string:string) m = ([ "A":"a", "B":"b", "C":"c" ]);
mapping(string:string) m = ([ "A":"a", "B":"b", "C":"c" ]);
foreach(m; string key; string value)
foreach(m; string key; string value)
Line 2,724: Line 3,035:
Result: bac
Result: bac


</syntaxhighlight>
</lang>


=={{header|PostScript}}==
=={{header|PostScript}}==
<lang postscript>
<syntaxhighlight lang="postscript">
% over keys and values
% over keys and values
<</a 1 /b 2 /c 3>> {= =} forall
<</a 1 /b 2 /c 3>> {= =} forall
Line 2,734: Line 3,045:
% just values
% just values
<</a 1 /b 2 /c 3>> {pop =} forall
<</a 1 /b 2 /c 3>> {pop =} forall
</syntaxhighlight>
</lang>


=={{header|Potion}}==
=={{header|Potion}}==
We can traverse tables by key or by key and val. We cannot traverse tables only by val.
We can traverse tables by key or by key and val. We cannot traverse tables only by val.
<lang potion>mydictionary = (red=0xff0000, green=0x00ff00, blue=0x0000ff)
<syntaxhighlight lang="potion">mydictionary = (red=0xff0000, green=0x00ff00, blue=0x0000ff)


mydictionary each (key, val): (key, ":", val, "\n") join print.
mydictionary each (key, val): (key, ":", val, "\n") join print.
mydictionary each (key): (key, "\n") join print.</lang>
mydictionary each (key): (key, "\n") join print.</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Using the following hash table:
Using the following hash table:
<lang powershell>$h = @{ 'a' = 1; 'b' = 2; 'c' = 3 }</lang>
<syntaxhighlight lang="powershell">$h = @{ 'a' = 1; 'b' = 2; 'c' = 3 }</syntaxhighlight>
Iterating over the key/value pairs is slightly cumbersome as it requires an explicit call to <code>GetEnumerator</code>:
Iterating over the key/value pairs is slightly cumbersome as it requires an explicit call to <code>GetEnumerator</code>:
<lang powershell>$h.GetEnumerator() | ForEach-Object { Write-Host Key: $_.Name, Value: $_.Value }</lang>
<syntaxhighlight lang="powershell">$h.GetEnumerator() | ForEach-Object { Write-Host Key: $_.Name, Value: $_.Value }</syntaxhighlight>
A <code>foreach</code> statement can also be used:
A <code>foreach</code> statement can also be used:
<lang powershell>foreach ($e in $h.GetEnumerator()) {
<syntaxhighlight lang="powershell">foreach ($e in $h.GetEnumerator()) {
Write-Host Key: $e.Name, Value: $e.Value
Write-Host Key: $e.Name, Value: $e.Value
}</lang>
}</syntaxhighlight>
Iterating over the keys:
Iterating over the keys:
<lang powershell>$h.Keys | ForEach-Object { Write-Host Key: $_ }
<syntaxhighlight lang="powershell">$h.Keys | ForEach-Object { Write-Host Key: $_ }


foreach ($k in $h.Keys) {
foreach ($k in $h.Keys) {
Write-Host Key: $k
Write-Host Key: $k
}</lang>
}</syntaxhighlight>
Iterating over the values:
Iterating over the values:
<lang powershell>$h.Values | ForEach-Object { Write-Host Value: $_ }
<syntaxhighlight lang="powershell">$h.Values | ForEach-Object { Write-Host Value: $_ }


foreach ($v in $h.Values) {
foreach ($v in $h.Values) {
Write-Host Value: $v
Write-Host Value: $v
}</lang>
}</syntaxhighlight>


=={{header|Prolog}}==
=={{header|Prolog}}==
Line 2,770: Line 3,081:
from having more than one value):
from having more than one value):


<lang prolog>
<syntaxhighlight lang="prolog">
assert( mymap(key1,value1) ).
assert( mymap(key1,value1) ).
assert( mymap(key2,value1) ).
assert( mymap(key2,value1) ).
</syntaxhighlight>
</lang>


To perform the specific task at hand:
To perform the specific task at hand:
<lang prolog>
<syntaxhighlight lang="prolog">
?- forall( mymap(Key,Value), writeln( [Key,Value]) ).
?- forall( mymap(Key,Value), writeln( [Key,Value]) ).


[key1,value1]
[key1,value1]
[key2,value1]
[key2,value1]
</syntaxhighlight>
</lang>


In Prolog, however, iteration is "built-in". For example:
In Prolog, however, iteration is "built-in". For example:
<lang prolog>
<syntaxhighlight lang="prolog">
?- mymap(key1, Y).
?- mymap(key1, Y).
Y = value1.
Y = value1.
Line 2,791: Line 3,102:
X = key1 ;
X = key1 ;
X = key2.
X = key2.
</syntaxhighlight>
</lang>


To construct the list of keys:
To construct the list of keys:
<lang prolog>
<syntaxhighlight lang="prolog">
?- findall( X, mymap(X,value1), Xs).
?- findall( X, mymap(X,value1), Xs).
Xs = [key1, key2].
Xs = [key1, key2].
</syntaxhighlight>
</lang>


To construct the list of distinct values:
To construct the list of distinct values:
<lang prolog>
<syntaxhighlight lang="prolog">
?- findall( Y, mymap(key1,Y), Ys).
?- findall( Y, mymap(key1,Y), Ys).
Ys = [value1].
Ys = [value1].
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
Hashes are a built-in type called Map in Purebasic.
Hashes are a built-in type called Map in Purebasic.


<lang purebasic>NewMap dict.s()
<syntaxhighlight lang="purebasic">NewMap dict.s()
dict("de") = "German"
dict("de") = "German"
dict("en") = "English"
dict("en") = "English"
Line 2,815: Line 3,126:
ForEach dict()
ForEach dict()
Debug MapKey(dict()) + ":" + dict()
Debug MapKey(dict()) + ":" + dict()
Next</lang>
Next</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>myDict = { "hello": 13,
<syntaxhighlight lang="python">myDict = { "hello": 13,
"world": 31,
"world": 31,
"!" : 71 }
"!" : 71 }
Line 2,835: Line 3,146:
# iterating over values:
# iterating over values:
for value in myDict.values():
for value in myDict.values():
print ("value = %s" % value)</lang>
print ("value = %s" % value)</syntaxhighlight>


=={{header|QB64}}==
<syntaxhighlight lang="qb64">
'dictionary is not native data type of QB64
' here a dictionary engine using a string to store data
Dim Shared Skey As String * 1, SValue As String * 1, EValue As String * 1
Skey = Chr$(0)
SValue = Chr$(1)
EValue = Chr$(255)
'Demo area---------------->
Dim MyDictionary As String
If ChangeValue(MyDictionary, "a", "Ananas") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "b", "Banana") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "c", "cherry") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "d", "Drake") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "e", "Elm") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "f", "Fire") Then Print "added new couple key value"
Print LenDict(MyDictionary)
Print "to key e there is "; GetDict$(MyDictionary, "e")
Print "to key e there is "; GetDict$(MyDictionary, "a")
If ChangeValue(MyDictionary, "e", "Elephant") Then Print " changed value of key passed"
Print "to key e there is "; GetDict$(MyDictionary, "e")
If Not (EraseKeyValue(MyDictionary, "e")) Then Print " Failed to erase key value passed" Else Print "Erased key value passed"
If GetDict$(MyDictionary, "e") = "" Then Print " No couple key value found for key value 'e'"
If ChangeKey(MyDictionary, "e", "f") = 0 Then
Print "key -a- has value "; GetDict$(MyDictionary, "a")
Print "we change key a to key e "
If ChangeKey(MyDictionary, "a", "e") = -1 Then
Print "key -a- has value "; GetDict$(MyDictionary, "a")
Print "key -e- has value "; GetDict$(MyDictionary, "e")
End If
End If
If InsertCouple(MyDictionary, "c", "m", "mellon") = -1 Then
Print " New couple inserted after key -c- "; GetDict$(MyDictionary, "c")
Print " new couple is key -m- "; GetDict$(MyDictionary, "m")
End If
Print LenDict(MyDictionary)
' End demo area --------------->
End
' it returns value/s for a key
Function GetDict$ (dict As String, Keys As String)
Dim StartK As Integer, StartV As Integer, EndV As Integer
StartK = InStr(dict, Skey + Keys + SValue)
StartV = InStr(StartK, dict, SValue)
EndV = InStr(StartV, dict, EValue)
If StartK = 0 Then GetDict$ = "" Else GetDict = Mid$(dict, StartV + 1, EndV - StartV)
End Function
' it changes value of a key or append the couple key, newvalue if key is new
Function ChangeValue (dict As String, Keys As String, NewValue As String)
ChangeValue = 0
Dim StartK As Integer, StartV As Integer, EndV As Integer
StartK = InStr(dict, Skey + Keys + SValue)
StartV = InStr(StartK, dict, SValue)
EndV = InStr(StartV, dict, EValue)
If StartK = 0 Then
dict = dict + Skey + Keys + SValue + NewValue + EValue
Else
dict = Left$(dict, StartV) + NewValue + Right$(dict, Len(dict) - EndV + 1)
End If
ChangeValue = -1
End Function
'it changes a key if it is in the dictionary
Function ChangeKey (dict As String, Keys As String, NewKey As String)
ChangeKey = 0
Dim StartK As Integer, StartV As Integer
StartK = InStr(dict, Skey + Keys + SValue)
StartV = InStr(StartK, dict, SValue)
If StartK = 0 Then
Print "Key " + Keys + " not found"
Exit Function
Else
dict = Left$(dict, StartK) + NewKey + Right$(dict, Len(dict) - StartV + 1)
End If
ChangeKey = -1
End Function
'it erases the couple key value
Function EraseKeyValue (dict As String, keys As String)
EraseKeyValue = 0
Dim StartK As Integer, StartV As Integer, EndV As Integer
StartK = InStr(dict, Skey + keys + SValue)
StartV = InStr(StartK, dict, SValue)
EndV = InStr(StartV, dict, EValue)
If StartK = 0 Then
Exit Function
Else
dict = Left$(dict, StartK - 1) + Right$(dict, Len(dict) - EndV + 1)
End If
EraseKeyValue = -1
End Function
'it inserts a couple after a defined key, if key is not in dictionary it append couple key value
Function InsertCouple (dict As String, SKeys As String, Keys As String, Value As String)
InsertCouple = 0
Dim StartK As Integer, StartV As Integer, EndV As Integer
StartK = InStr(dict, Skey + SKeys + SValue)
StartV = InStr(StartK, dict, SValue)
EndV = InStr(StartV, dict, EValue)
If StartK = 0 Then
dict = dict + Skey + Keys + SValue + Value + EValue
Else
dict = Left$(dict, EndV) + Skey + Keys + SValue + Value + EValue + Right$(dict, Len(dict) - EndV + 1)
End If
InsertCouple = -1
End Function
Function LenDict (dict As String)
LenDict = 0
Dim a As Integer, count As Integer
If Len(dict) <= 0 Then Exit Function
While a <= Len(dict)
a = InStr(a + 1, dict, EValue)
If a > 0 Then count = count + 1 Else Exit While
Wend
LenDict = count
End Function

</syntaxhighlight>
=={{header|R}}==
=={{header|R}}==


Line 2,843: Line 3,277:
=== environment example ===
=== environment example ===


<lang r>> env <- new.env()
<syntaxhighlight lang="r">> env <- new.env()
> env[["x"]] <- 123
> env[["x"]] <- 123
> env[["x"]]</lang>
> env[["x"]]</syntaxhighlight>
<pre>[1] 123</pre>
<pre>[1] 123</pre>
<lang r>> index <- "1"
<syntaxhighlight lang="r">> index <- "1"
> env[[index]] <- "rainfed hay"
> env[[index]] <- "rainfed hay"
> for (name in ls(env)) {
> for (name in ls(env)) {
+ cat(sprintf('index=%s, value=%s\n', name, env[[name]]))
+ cat(sprintf('index=%s, value=%s\n', name, env[[name]]))
+ }</lang>
+ }</syntaxhighlight>
<pre>index=1, value=rainfed hay
<pre>index=1, value=rainfed hay
index=x, value=123</pre>
index=x, value=123</pre>
Line 2,857: Line 3,291:
=== vector example ===
=== vector example ===


<lang r>> x <- c(hello=1, world=2, "!"=3)
<syntaxhighlight lang="r">> x <- c(hello=1, world=2, "!"=3)
> print(x["!"])</lang>
> print(x["!"])</syntaxhighlight>
<pre>!
<pre>!
3</pre>
3</pre>
<lang r>> print(unname(x["!"]))</lang>
<syntaxhighlight lang="r">> print(unname(x["!"]))</syntaxhighlight>
<pre>[1] 3</pre>
<pre>[1] 3</pre>


=== list example ===
=== list example ===


<lang R>> a <- list(a=1, b=2, c=3.14, d="xyz")
<syntaxhighlight lang="r">> a <- list(a=1, b=2, c=3.14, d="xyz")
> print(a$a)</lang>
> print(a$a)</syntaxhighlight>
<pre>[1] 1</pre>
<pre>[1] 1</pre>
<lang R>> print(a$d)</lang>
<syntaxhighlight lang="r">> print(a$d)</syntaxhighlight>
<pre>[1] "xyz"</pre>
<pre>[1] "xyz"</pre>


Line 2,875: Line 3,309:
Using the dictionary interface, different data structures can be treated as an associative array in Racket.
Using the dictionary interface, different data structures can be treated as an associative array in Racket.


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 2,886: Line 3,320:
(for/list ([(k v) (in-dict dict3)]) ; => '("0 -> a" "1 -> b" "2 -> c")
(for/list ([(k v) (in-dict dict3)]) ; => '("0 -> a" "1 -> b" "2 -> c")
(format "~a -> ~a" k v))
(format "~a -> ~a" k v))
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,892: Line 3,326:
{{works with|Rakudo|2015.12}}
{{works with|Rakudo|2015.12}}


<lang perl6>my %pairs = hello => 13, world => 31, '!' => 71;
<syntaxhighlight lang="raku" line>my %pairs = hello => 13, world => 31, '!' => 71;
for %pairs.kv -> $k, $v {
for %pairs.kv -> $k, $v {
Line 2,907: Line 3,341:
say "key = $_" for %pairs.keys;
say "key = $_" for %pairs.keys;


say "value = $_" for %pairs.values;</lang>
say "value = $_" for %pairs.values;</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program demonstrates how to set and display values for an associative array. */
<syntaxhighlight lang="rexx">/*REXX program demonstrates how to set and display values for an associative array. */
/*╔════════════════════════════════════════════════════════════════════════════════════╗
/*╔════════════════════════════════════════════════════════════════════════════════════╗
║ The (below) two REXX statements aren't really necessary, but it shows how to ║
║ The (below) two REXX statements aren't really necessary, but it shows how to ║
Line 2,967: Line 3,401:
stateN.code= name; w= max(w,length(name)) /*define the state's name; max width. */
stateN.code= name; w= max(w,length(name)) /*define the state's name; max width. */
stateC.code= cap /* " " " code to the capital*/
stateC.code= cap /* " " " code to the capital*/
return /*return to invoker, SETSC is finished.*/</lang>
return /*return to invoker, SETSC is finished.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
<pre>
Line 3,000: Line 3,434:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Associative array/Iteration
# Project : Associative array/Iteration


Line 3,007: Line 3,441:
see lst[n][1] + " : " + lst[n][2] + nl
see lst[n][1] + " : " + lst[n][2] + nl
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,018: Line 3,452:
Associative arrays are called ''lists'' in RLaB.
Associative arrays are called ''lists'' in RLaB.


<syntaxhighlight lang="rlab">
<lang RLaB>
x = <<>>; // create an empty list
x = <<>>; // create an empty list
x.hello = 1;
x.hello = 1;
Line 3,043: Line 3,477:




</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>my_dict = { "hello" => 13,
<syntaxhighlight lang="ruby">my_dict = { "hello" => 13,
"world" => 31,
"world" => 31,
"!" => 71 }
"!" => 71 }
Line 3,059: Line 3,493:


# iterating over values:
# iterating over values:
my_dict.each_value {|value| puts "value =#{value}"}</lang>
my_dict.each_value {|value| puts "value =#{value}"}</syntaxhighlight>


another way:
another way:
<lang ruby>for key, value in my_dict
<syntaxhighlight lang="ruby">for key, value in my_dict
puts "key = #{key}, value = #{value}"
puts "key = #{key}, value = #{value}"
end
end
Line 3,072: Line 3,506:
for value in my_dict.values
for value in my_dict.values
puts "value = #{value}"
puts "value = #{value}"
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 3,088: Line 3,522:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>use std::collections::HashMap;
<syntaxhighlight lang="rust">use std::collections::HashMap;
fn main() {
fn main() {
let mut olympic_medals = HashMap::new();
let mut olympic_medals = HashMap::new();
Line 3,100: Line 3,534:
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Note that <code>HashMap</code> does not preserve order (if this is important, <code>std::collections::BTreeMap</code> is what you want.)
Note that <code>HashMap</code> does not preserve order (if this is important, <code>std::collections::BTreeMap</code> is what you want.)
Line 3,111: Line 3,545:


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>val m = Map("Amsterdam" -> "Netherlands", "New York" -> "USA", "Heemstede" -> "Netherlands")
<syntaxhighlight lang="scala">val m = Map("Amsterdam" -> "Netherlands", "New York" -> "USA", "Heemstede" -> "Netherlands")


println(f"Key->Value: ${m.mkString(", ")}%s")
println(f"Key->Value: ${m.mkString(", ")}%s")
Line 3,117: Line 3,551:
println(f"Keys: ${m.keys.mkString(", ")}%s")
println(f"Keys: ${m.keys.mkString(", ")}%s")
println(f"Values: ${m.values.mkString(", ")}%s")
println(f"Values: ${m.values.mkString(", ")}%s")
println(f"Unique values: ${m.values.toSet.mkString(", ")}%s")</lang>{{out}}<pre>
println(f"Unique values: ${m.values.toSet.mkString(", ")}%s")</syntaxhighlight>{{out}}<pre>
Key->Value: Amsterdam -> Netherlands, New York -> USA, Heemstede -> Netherlands
Key->Value: Amsterdam -> Netherlands, New York -> USA, Heemstede -> Netherlands
Pairs: (Amsterdam,Netherlands), (New York,USA), (Heemstede,Netherlands)
Pairs: (Amsterdam,Netherlands), (New York,USA), (Heemstede,Netherlands)
Line 3,129: Line 3,563:
{{works with|Gauche Scheme}}
{{works with|Gauche Scheme}}


<syntaxhighlight lang="scheme">
<lang Scheme>
;; Create an associative array (hash-table) whose keys are strings:
;; Create an associative array (hash-table) whose keys are strings:
(define table (hash-table 'string=?
(define table (hash-table 'string=?
Line 3,140: Line 3,574:
;; Create by "partial application" a function that accepts 2 arguments,
;; Create by "partial application" a function that accepts 2 arguments,
;; the key and the value:
;; the key and the value:
(pa$ format #t "Key = ~a, Value = ~a\n"))</lang>
(pa$ format #t "Key = ~a, Value = ~a\n"))</syntaxhighlight>


Output:
Output:
Line 3,149: Line 3,583:
</pre>
</pre>


<syntaxhighlight lang="scheme">
<lang Scheme>
;; Iterate over the table and create a list of the keys and the
;; Iterate over the table and create a list of the keys and the
;; altered values:
;; altered values:
Line 3,161: Line 3,595:
(lambda (k-v) (cons (car k-v) (+ (cdr k-v) 5000)))
(lambda (k-v) (cons (car k-v) (+ (cdr k-v) 5000)))
table)
table)
</syntaxhighlight>
</lang>


To get a list of the keys or of the values of the table,
To get a list of the keys or of the values of the table,
Line 3,170: Line 3,604:
(hash-table-values table)
(hash-table-values table)
</pre>
</pre>

=== For [[Associative_array/Creation#A_persistent_associative_array_from_scratch|''persistent'' associative arrays]] ===
{{works with|CHICKEN|5.3.0}}
{{libheader|r7rs}}
{{libheader|srfi-1}}

Here is a variant of [[Associative_array/Creation#A_persistent_associative_array_from_scratch|''persistent'' associative arrays]] that includes ''generators''. Such generators are functionally equivalent to iterators; the only important difference is they are executable procedures rather than passive objects. Note also that, because these associative arrays are persistent, iterating through their structure is much safer than it would be in a conventional hash table, which might change or even disappear while you were doing the iteration.

What I present here is a trimmed-down version of what you might find in a prefabricated library, such as an implementation of SRFI-125. I suppose part of my point in presenting this is that ‘associative arrays’ are not actually part of the Scheme language (even in R6RS), but rather are library add-ons. Someone else has done the sort of thing I am demonstrating here. This is in contrast to, say, Awk, or Icon, where associative ‘arrays’ ''really are'' built into the language.

(To save space, I have removed comments you can read at the section for [[Associative_array/Creation#A_persistent_associative_array_from_scratch|''persistent'' associative arrays]].)

<syntaxhighlight lang="scheme">(cond-expand
(r7rs)
(chicken (import r7rs)))

(define-library (suspendable-procedures)

(export &fail failure? success? suspend fail-forever
make-generator-procedure)

(import (scheme base))

(begin

(define-record-type <&fail>
(make-the-one-unique-&fail-that-you-must-not-make-twice)
do-not-use-this:&fail?)

(define &fail
(make-the-one-unique-&fail-that-you-must-not-make-twice))

(define (failure? f) (eq? f &fail))
(define (success? f) (not (failure? f)))
(define *suspend* (make-parameter (lambda (x) x)))
(define (suspend v) ((*suspend*) v))
(define (fail-forever)
(let loop ()
(suspend &fail)
(loop)))

(define (make-generator-procedure thunk)
;; This is for making a suspendable procedure that takes no
;; arguments when resumed. The result is a simple generator of
;; values.
(define (next-run return)
(define (my-suspend v)
(set! return (call/cc (lambda (resumption-point)
(set! next-run resumption-point)
(return v)))))
(parameterize ((*suspend* my-suspend))
(suspend (thunk))
(fail-forever)))
(lambda () (call/cc next-run)))

)) ;; end library (suspendable-procedures)

(define-library (avl-trees)
;;
;; Persistent (that is, ‘immutable’) AVL trees for R7RS Scheme.
;;
;; References:
;;
;; * Niklaus Wirth, 1976. Algorithms + Data Structures =
;; Programs. Prentice-Hall, Englewood Cliffs, New Jersey.
;;
;; * Niklaus Wirth, 2004. Algorithms and Data Structures. Updated
;; by Fyodor Tkachov, 2014.
;;

(export avl-make-generator)
(export avl avl? avl-empty? avl-insert avl-search-values)
(export avl-check-usage)

(import (scheme base))
(import (scheme case-lambda))
(import (scheme process-context))
(import (scheme write))
(import (suspendable-procedures))

(begin

(define-syntax avl-check-usage
(syntax-rules ()
((_ pred msg)
(or pred (usage-error msg)))))

(define-record-type <avl>
(%avl key data bal left right)
avl?
(key %key)
(data %data)
(bal %bal)
(left %left)
(right %right))

(define avl-make-generator
(case-lambda
((tree) (avl-make-generator tree 1))
((tree direction)
(if (negative? direction)
(make-generator-procedure
(lambda ()
(define (traverse p)
(unless (or (not p) (avl-empty? p))
(traverse (%right p))
(suspend (cons (%key p) (%data p)))
(traverse (%left p)))
&fail)
(traverse tree)))
(make-generator-procedure
(lambda ()
(define (traverse p)
(unless (or (not p) (avl-empty? p))
(traverse (%left p))
(suspend (cons (%key p) (%data p)))
(traverse (%right p)))
&fail)
(traverse tree)))))))

(define (avl) (%avl #f #f #f #f #f))

(define (avl-empty? tree)
(avl-check-usage
(avl? tree)
"avl-empty? expects an AVL tree as argument")
(not (%bal tree)))

(define (avl-search-values pred<? tree key)
(define (search p)
(if (not p)
(values #f #f)
(let ((k (%key p)))
(cond ((pred<? key k) (search (%left p)))
((pred<? k key) (search (%right p)))
(else (values (%data p) #t))))))
(avl-check-usage
(procedure? pred<?)
"avl-search-values expects a procedure as first argument")
(if (avl-empty? tree)
(values #f #f)
(search tree)))

(define (avl-insert pred<? tree key data)
(define (search p fix-balance?)
(cond
((not p)
(values (%avl key data 0 #f #f) #t))
((pred<? key (%key p))
(let-values (((p1 fix-balance?)
(search (%left p) fix-balance?)))
(cond
((not fix-balance?)
(let ((p^ (%avl (%key p) (%data p) (%bal p)
p1 (%right p))))
(values p^ #f)))
(else
(case (%bal p)
((1)
(let ((p^ (%avl (%key p) (%data p) 0
p1 (%right p))))
(values p^ #f)))
((0)
(let ((p^ (%avl (%key p) (%data p) -1
p1 (%right p))))
(values p^ fix-balance?)))
((-1)
(case (%bal p1)
((-1)
(let* ((p^ (%avl (%key p) (%data p) 0
(%right p1) (%right p)))
(p1^ (%avl (%key p1) (%data p1) 0
(%left p1) p^)))
(values p1^ #f)))
((0 1)
(let* ((p2 (%right p1))
(bal2 (%bal p2))
(p^ (%avl (%key p) (%data p)
(- (min bal2 0))
(%right p2) (%right p)))
(p1^ (%avl (%key p1) (%data p1)
(- (max bal2 0))
(%left p1) (%left p2)))
(p2^ (%avl (%key p2) (%data p2) 0
p1^ p^)))
(values p2^ #f)))
(else (internal-error))))
(else (internal-error)))))))

((pred<? (%key p) key)
(let-values (((p1 fix-balance?)
(search (%right p) fix-balance?)))
(cond
((not fix-balance?)
(let ((p^ (%avl (%key p) (%data p) (%bal p)
(%left p) p1)))
(values p^ #f)))
(else
(case (%bal p)
((-1)
(let ((p^ (%avl (%key p) (%data p) 0
(%left p) p1)))
(values p^ #f)))
((0)
(let ((p^ (%avl (%key p) (%data p) 1
(%left p) p1)))
(values p^ fix-balance?)))
((1)
(case (%bal p1)
((1)
(let* ((p^ (%avl (%key p) (%data p) 0
(%left p) (%left p1)))
(p1^ (%avl (%key p1) (%data p1) 0
p^ (%right p1))))
(values p1^ #f)))
((-1 0)
(let* ((p2 (%left p1))
(bal2 (%bal p2))
(p^ (%avl (%key p) (%data p)
(- (max bal2 0))
(%left p) (%left p2)))
(p1^ (%avl (%key p1) (%data p1)
(- (min bal2 0))
(%right p2) (%right p1)))
(p2^ (%avl (%key p2) (%data p2) 0
p^ p1^)))
(values p2^ #f)))
(else (internal-error))))
(else (internal-error)))))))
(else
(values (%avl key data (%bal p) (%left p) (%right p))
#f))))
(avl-check-usage
(procedure? pred<?)
"avl-insert expects a procedure as first argument")
(if (avl-empty? tree)
(%avl key data 0 #f #f)
(let-values (((p fix-balance?) (search tree #f)))
p)))

(define (internal-error)
(display "internal error\n" (current-error-port))
(emergency-exit 123))

(define (usage-error msg)
(display "Procedure usage error:\n" (current-error-port))
(display " " (current-error-port))
(display msg (current-error-port))
(newline (current-error-port))
(exit 1))

)) ;; end library (avl-trees)


(define-library (associative-arrays)
;;
;; Persistent associative ‘arrays’ for R7RS Scheme.
;;
;; The structure is not actually an array, but is made of AVL trees
;; and association lists. Given a good hash function, it should
;; average logarithmic performance.
;;

(export assoc-array-make-pair-generator
assoc-array-make-key-generator
assoc-array-make-data-generator)
(export assoc-array assoc-array? assoc-array-set assoc-array-ref)

(import (scheme base))
(import (scheme case-lambda))
(import (scheme write))
(import (suspendable-procedures))
(import (avl-trees))

(cond-expand
(chicken (import (only (srfi 1) alist-delete)))
;; Insert whatever you need here for your Scheme.
(else))

(begin

(define-record-type <assoc-array>
(%assoc-array hashfunc pred=? default table)
assoc-array?
(hashfunc %hashfunc)
(pred=? %pred=?)
(default %default)
(table %table))

(define (assoc-array-make-generator array kind)
(define tree-traverser (avl-make-generator (%table array)))
(define get-desired-part
(cond ((eq? kind 'key) (lambda (pair) (car pair)))
((eq? kind 'data) (lambda (pair) (cdr pair)))
(else (lambda (pair) pair))))
(make-generator-procedure
(lambda ()
(let traverse ()
(let ((tree-entry (tree-traverser)))
(when (success? tree-entry)
(let scan-lst ((lst (cdr tree-entry)))
(when (pair? lst)
(suspend (get-desired-part (car lst)))
(scan-lst (cdr lst))))
(traverse))))
&fail)))

(define (assoc-array-make-pair-generator array)
(assoc-array-make-generator array 'pair))

(define (assoc-array-make-key-generator array)
(assoc-array-make-generator array 'key))

(define (assoc-array-make-data-generator array)
(assoc-array-make-generator array 'data))

(define assoc-array
(case-lambda
((hashfunc)
(let ((pred=? equal?)
(default #f))
(assoc-array hashfunc pred=? default)))
((hashfunc pred=?)
(let ((default #f))
(assoc-array hashfunc pred=? default)))
((hashfunc pred=? default)
(%assoc-array hashfunc pred=? default (avl)))))

(define (assoc-array-set array key data)
(let ((hashfunc (%hashfunc array))
(pred=? (%pred=? array))
(default (%default array))
(table (%table array)))
(let ((hash-value (hashfunc key)))
(let*-values
(((alst found?) (avl-search-values < table hash-value)))
(cond
(found?
(let* ((alst (alist-delete key alst pred=?))
(alst `((,key . ,data) . ,alst))
(table (avl-insert < table hash-value alst)))
(%assoc-array hashfunc pred=? default table)))
(else
(let* ((alst `((,key . ,data)))
(table (avl-insert < table hash-value alst)))
(%assoc-array hashfunc pred=? default table))))))))

(define (assoc-array-ref array key)
(let* ((hashfunc (%hashfunc array))
(hash-value (hashfunc key)))
(let*-values
(((alst found?)
(avl-search-values < (%table array) hash-value)))
(if found?
(let ((pair (assoc key alst (%pred=? array))))
(if pair
(cdr pair)
(%default array)))
(%default array)))))

)) ;; end library (associative-arrays)


(cond-expand
(DEMONSTRATION
(begin
(import (scheme base))
(import (scheme write))
(import (suspendable-procedures))
(import (associative-arrays))

(define (hashfunc s)
;; Using Knuth’s random number generator to concoct a quick and
;; dirty and probably very bad hash function. It should be much
;; better to use something like SpookyHash, but this is a demo.
(define a 6364136223846793005)
(define c 1442695040888963407)
(define M (expt 2 64))
(let ((n (string-length s))
(h 123))
(do ((i 0 (+ i 1)))
((= i n))
(let* ((x (char->integer (string-ref s i)))
(x (+ (* a (+ h x)) c)))
(set! h (truncate-remainder x M))))
h))

(define a (assoc-array hashfunc))

;; Fill the associative array ‘a’ with (string . number)
;; associations.
(do ((i 1 (+ i 1)))
((= i 11))
(set! a (assoc-array-set a (number->string i) i)))

;; Go through the association pairs (in arbitrary order) with a
;; generator.
(let ((gen (assoc-array-make-pair-generator a)))
(do ((pair (gen) (gen)))
((failure? pair))
(write pair) (display " "))
(newline))

;; Go through the keys (in arbitrary order) with a generator.
(let ((gen (assoc-array-make-key-generator a)))
(do ((key (gen) (gen)))
((failure? key))
(write key) (display " "))
(newline))

;; Go through the values (in arbitrary order) with a generator.
(let ((gen (assoc-array-make-data-generator a)))
(do ((value (gen) (gen)))
((failure? value))
(write value) (display " "))
(newline))

))
(else))</syntaxhighlight>
{{out}}
<pre>$ csc -DDEMONSTRATION -R r7rs -X r7rs associative_array_with_generators.scm && ./associative_array_with_generators
("3" . 3) ("6" . 6) ("9" . 9) ("1" . 1) ("4" . 4) ("7" . 7) ("2" . 2) ("10" . 10) ("5" . 5) ("8" . 8)
"3" "6" "9" "1" "4" "7" "2" "10" "5" "8"
3 6 9 1 4 7 2 10 5 8</pre>


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const type: dictType is hash [string] integer;
const type: dictType is hash [string] integer;
Line 3,200: Line 4,058:
writeln("value = " <& number);
writeln("value = " <& number);
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 3,213: Line 4,071:
value = 1
value = 1
value = 2
value = 2
</pre>

=={{header|SenseTalk}}==
<syntaxhighlight lang="sensetalk">put {name:"Fluffy", type:"Rabbit", color:"White"} into animal
put "Carries a watch" into animal's habits

put "The animal: " & animal
put "The keys: " & keys of animal
put "The values: " & animal's values
// Keys and Values
put ,"All Properties:"
repeat with each [key,value] in animal
put !"Key: [[key]] Value: [[value]]"
end repeat

// Keys only
put ,"Keys:"
repeat with each [key] in animal
put key
end repeat

// Values only
put ,"Values:"
repeat with each [,value] in animal
put value
end repeat

// Using an iterator
put ,"Treating the property list as an iterator:"
put animal's nextValue -- calling any of the "next" functions begins iteration
put animal's nextKeyValue
put animal's nextKey
put animal's nextKeyValue
put animal's nextValue -- walking off the end returns a unique endValue
</syntaxhighlight>
{{out}}
<pre>
The animal: {color:"White", habits:"Carries a watch", name:"Fluffy", type:"Rabbit"}
The keys: ["color","habits","name","type"]
The values: ["White","Carries a watch","Fluffy","Rabbit"]

All Properties:
Key: color Value: White
Key: habits Value: Carries a watch
Key: name Value: Fluffy
Key: type Value: Rabbit

Keys:
color
habits
name
type

Values:
White
Carries a watch
Fluffy
Rabbit

Treating the property list as an iterator:
White
["habits","Carries a watch"]
name
["type","Rabbit"]
    ⓔ ⓝ ⓓ    
</pre>
</pre>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var hash = Hash.new(
<syntaxhighlight lang="ruby">var hash = Hash(
key1 => 'value1',
key1 => 'value1',
key2 => 'value2',
key2 => 'value2',
Line 3,223: Line 4,146:
# Iterate over key-value pairs
# Iterate over key-value pairs
hash.each { |key, value|
hash.each { |key, value|
say "#{key}: #{value}";
say "#{key}: #{value}"
}
}


# Iterate only over keys
# Iterate only over keys
hash.keys.each { |key|
hash.keys.each { |key|
say key;
say key
}
}


# Iterate only over values
# Iterate only over values
hash.values.each { |value|
hash.values.each { |value|
say value;
say value
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>key1: value1
<pre>key1: value1
Line 3,246: Line 4,169:
=={{header|Slate}}==
=={{header|Slate}}==
In Slate, all associative mappings inherit from <tt>Mapping</tt>, so they all have the same protocol. Even <tt>Sequence</tt>s obey it, in addition to their own protocol for collections with ordered integer-range keys.
In Slate, all associative mappings inherit from <tt>Mapping</tt>, so they all have the same protocol. Even <tt>Sequence</tt>s obey it, in addition to their own protocol for collections with ordered integer-range keys.
<lang slate>define: #pairs -> ({'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3} as: Dictionary).
<syntaxhighlight lang="slate">define: #pairs -> ({'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3} as: Dictionary).
pairs keysAndValuesDo: [| :key :value |
pairs keysAndValuesDo: [| :key :value |
inform: '(k, v) = (' ; key printString ; ', ' ; value printString ; ')'
inform: '(k, v) = (' ; key printString ; ', ' ; value printString ; ')'
Line 3,257: Line 4,180:
pairs do: [| :value |
pairs do: [| :value |
inform: 'value = ' ; value printString
inform: 'value = ' ; value printString
].</lang>
].</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
{{works with|GNU Smalltalk}}


<lang smalltalk>|pairs|
<syntaxhighlight lang="smalltalk">|pairs|
pairs := Dictionary
pairs := Dictionary
from: { 'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3 }.
from: { 'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3 }.
Line 3,279: Line 4,202:
pairs do: [ :value |
pairs do: [ :value |
('value = %1' % { value }) displayNl
('value = %1' % { value }) displayNl
].</lang>
].</syntaxhighlight>


We could also obtain a set of keys or a collection of values and iterate over them with "<tt>do:</tt>":
We could also obtain a set of keys or a collection of values and iterate over them with "<tt>do:</tt>":


<lang smalltalk>(pairs keys) do: [ :k | "..." ].
<syntaxhighlight lang="smalltalk">(pairs keys) do: [ :k | "..." ].
(pairs values) do: [ :v | "..." ].</lang>
(pairs values) do: [ :v | "..." ].</syntaxhighlight>


=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
Line 3,292: Line 4,215:
{{works with|CSnobol}}
{{works with|CSnobol}}


<lang SNOBOL4>* # Create sample table
<syntaxhighlight lang="snobol4">* # Create sample table
t = table()
t = table()
t<'cat'> = 'meow'
t<'cat'> = 'meow'
Line 3,307: Line 4,230:
* # Iterate vals
* # Iterate vals
vloop k = k + 1; output = a<k,2> :s(vloop)
vloop k = k + 1; output = a<k,2> :s(vloop)
end</lang>
end</syntaxhighlight>


=={{header|Stata}}==
=={{header|Stata}}==
<lang stata>mata
<syntaxhighlight lang="stata">mata
// Create an associative array
// Create an associative array
a=asarray_create()
a=asarray_create()
Line 3,322: Line 4,245:
loc=asarray_next(a,loc)
loc=asarray_next(a,loc)
} while(loc!=NULL)
} while(loc!=NULL)
end</lang>
end</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>let myMap = [
<syntaxhighlight lang="swift">let myMap = [
"hello": 13,
"hello": 13,
"world": 31,
"world": 31,
Line 3,332: Line 4,255:
// iterating over key-value pairs:
// iterating over key-value pairs:
for (key, value) in myMap {
for (key, value) in myMap {
println("key = \(key), value = \(value)")
print("key = \(key), value = \(value)")
}
}</lang>
// Just the keys
for key in myMap.keys
{
print("key = \(key)")
}
// Just the values
for value in myMap.values
{
print("value = \(value)")
}</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
===With Arrays===
===With Arrays===
<lang tcl>array set myAry {
<syntaxhighlight lang="tcl">array set myAry {
# list items here...
# list items here...
}
}
Line 3,352: Line 4,285:


# There is nothing for directly iterating over just the values
# There is nothing for directly iterating over just the values
# Use the keys+values version and ignore the keys</lang>
# Use the keys+values version and ignore the keys</syntaxhighlight>
===With Dictionaries===
===With Dictionaries===
{{works with|Tcl|8.5}}
{{works with|Tcl|8.5}}
<lang tcl>set myDict [dict create ...]; # Make the dictionary
<syntaxhighlight lang="tcl">set myDict [dict create ...]; # Make the dictionary


# Iterate over keys and values
# Iterate over keys and values
Line 3,370: Line 4,303:
foreach value [dict values $myDict] {
foreach value [dict values $myDict] {
puts "value = $value"
puts "value = $value"
}</lang>
}</syntaxhighlight>


=={{header|TXR}}==
=={{header|TXR}}==


<lang txrlisp>(defvarl h (hash))
<syntaxhighlight lang="txrlisp">(defvarl h (hash))


(each ((k '(a b c))
(each ((k '(a b c))
Line 3,381: Line 4,314:


(dohash (k v h)
(dohash (k v h)
(put-line `@k -> @v`))</lang>
(put-line `@k -> @v`))</syntaxhighlight>


{{out|Run}}
{{out|Run}}
Line 3,391: Line 4,324:


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Two shells have associative arrays, but they use different syntax to access their keys.
At least two shells have associative arrays, but they use different syntax to access their keys.


{{works with|ksh93}}
{{works with|ksh93}}
{{works with|bash|4.0 and above}}
<lang bash>typeset -A a=([key1]=value1 [key2]=value2)
<syntaxhighlight lang="bash">typeset -A a=([key1]=value1 [key2]=value2)


# just keys
# just keys
Line 3,405: Line 4,339:
for key in "${!a[@]}"; do
for key in "${!a[@]}"; do
printf '%s => %s\n' "$key" "${a[$key]}"
printf '%s => %s\n' "$key" "${a[$key]}"
done</lang>
done</syntaxhighlight>


{{works with|zsh}}
{{works with|zsh}}
<lang bash>typeset -A a
<syntaxhighlight lang="bash">typeset -A a
a=(key1 value1 key2 value2)
a=(key1 value1 key2 value2)


Line 3,418: Line 4,352:


# keys and values
# keys and values
printf '%s => %s\n' ${(kv)a}</lang>
printf '%s => %s\n' ${(kv)a}</syntaxhighlight>


=={{header|Vala}}==
=={{header|Vala}}==
{{libheader|Gee}}
{{libheader|Gee}}
<lang vala>
<syntaxhighlight lang="vala">
using Gee;
using Gee;


Line 3,452: Line 4,386:
}
}
}
}
</syntaxhighlight>
</lang>


Compile with flag:
Compile with flag:
Line 3,475: Line 4,409:
Dictionaries are similar in VBA and VBScript. Here is how to iterate.
Dictionaries are similar in VBA and VBScript. Here is how to iterate.


<lang vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit
Sub Test()
Sub Test()
Dim h As Object, i As Long, u, v, s
Dim h As Object, i As Long, u, v, s
Line 3,499: Line 4,433:
Debug.Print u(i), v(i)
Debug.Print u(i), v(i)
Next
Next
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
'instantiate the dictionary object
'instantiate the dictionary object
Set dict = CreateObject("Scripting.Dictionary")
Set dict = CreateObject("Scripting.Dictionary")
Line 3,515: Line 4,449:
WScript.StdOut.WriteLine key & " - " & dict.Item(key)
WScript.StdOut.WriteLine key & " - " & dict.Item(key)
Next
Next
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 3,525: Line 4,459:


=={{header|Vim Script}}==
=={{header|Vim Script}}==
<lang vim>let dict = {"apples": 11, "oranges": 25, "pears": 4}
<syntaxhighlight lang="vim">let dict = {"apples": 11, "oranges": 25, "pears": 4}


echo "Iterating over key-value pairs"
echo "Iterating over key-value pairs"
Line 3,542: Line 4,476:
for value in values(dict)
for value in values(dict)
echo value
echo value
endfor</lang>
endfor</syntaxhighlight>


{{Out}}
{{Out}}
Line 3,559: Line 4,493:
4
4
11</pre>
11</pre>

=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
my_map := {
"hello": 13,
"world": 31,
"!" : 71 }
// iterating over key-value pairs:
for key, value in my_map {
println("key = $key, value = $value")
}
// iterating over keys:
for key,_ in my_map {
println("key = $key")
}
// iterating over values:
for _, value in my_map {
println("value = $value")
}
}</syntaxhighlight>

{{out}}
<pre>key = hello, value = 13
key = world, value = 31
key = !, value = 71
key = hello
key = world
key = !
value = 13
value = 31
value = 71
</pre>


=={{header|Wart}}==
=={{header|Wart}}==
<lang wart>h <- (table 'a 1 'b 2)
<syntaxhighlight lang="wart">h <- (table 'a 1 'b 2)
each (key val) table
each (key val) table
prn key " " val</lang>
prn key " " val</syntaxhighlight>


{{out}}
{{out}}
Line 3,571: Line 4,540:
=={{header|Wren}}==
=={{header|Wren}}==
Note that Wren makes no guarantee about iteration order which is not necessarily the same order in which the entries were added.
Note that Wren makes no guarantee about iteration order which is not necessarily the same order in which the entries were added.
<lang ecmascript>// create a new map with four entries
<syntaxhighlight lang="wren">// create a new map with four entries
var capitals = {
var capitals = {
"France": "Paris",
"France": "Paris",
Line 3,588: Line 4,557:


// iterate though the map and print out just the values
// iterate though the map and print out just the values
for (v in capitals.values) System.print(v)</lang>
for (v in capitals.values) System.print(v)</syntaxhighlight>


{{out}}
{{out}}
Line 3,609: Line 4,578:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\stdlib;
<syntaxhighlight lang="xpl0">include c:\cxpl\stdlib;
char Dict(10,10);
char Dict(10,10);
int Entries;
int Entries;
Line 3,628: Line 4,597:
for I:= 0 to Entries-1 do
for I:= 0 to Entries-1 do
[ChOut(0, Dict(I,0)); ChOut(0, ^ ); Text(0, @Dict(I,1)); CrLf(0)];
[ChOut(0, Dict(I,0)); ChOut(0, ^ ); Text(0, @Dict(I,1)); CrLf(0)];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,639: Line 4,608:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>var d=Dictionary("A","alpha","D","delta", "B","beta", "C", "gamma");
<syntaxhighlight lang="zkl">var d=Dictionary("A","alpha","D","delta", "B","beta", "C", "gamma");
d.keys.pump(Console.print,fcn(k){String(k,",")})
d.keys.pump(Console.print,fcn(k){String(k,",")})
d.values.apply("toUpper").println();
d.values.apply("toUpper").println();
d.makeReadOnly(); // can only iterate over k,v pairs if read only
d.makeReadOnly(); // can only iterate over k,v pairs if read only
foreach k,v in (d){print(k,":",v,"; ")}</lang>
foreach k,v in (d){print(k,":",v,"; ")}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Latest revision as of 08:07, 24 January 2024

Task
Associative array/Iteration
You are encouraged to solve this task according to the task description, using any language you may know.

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.


See also



11l

V d = [‘key1’ = ‘value1’, ‘key2’ = ‘value2’]

L(key, value) d
   print(key‘ = ’value)

L(key) d.keys()
   print(key)

L(value) d.values()
   print(value)
Output:
key1 = value1
key2 = value2
key1
key2
value1
value2

8th

Iterating key,value pairs uses "m:each":

{"one": 1, "two": "bad"}
( swap . space . cr )
m:each
Output:

one 1 two bad

Iterating the keys uses "m:keys":

{"one": 1, "two": "bad"} m:keys
( . cr )
a:each
Output:

one two

Ada

with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Test_Iteration is
   package String_Maps is
      new Ada.Containers.Indefinite_Ordered_Maps (String, Integer);
   use String_Maps;
   A     : Map;
   Index : Cursor;
begin
   A.Insert ("hello", 1);
   A.Insert ("world", 2);
   A.Insert ("!",     3);
   Index := A.First;
   while Index /= No_Element loop
      Put_Line (Key (Index) & Integer'Image (Element (Index)));
      Index := Next (Index);
   end loop;
end Test_Iteration;
Output:
! 3
hello 1
world 2

Aime

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_form("key ~, value ~ (~)\n", s, r[s], r_type(r, s));
    } while (rsk_greater(r, s, s));
}
Output:
key A, value 33 (integer)
key B, value associative (text)
key C, value 2.5 (real)

ALGOL 68

Algol 68 does not have associative arrays as standard.
This sample defines a simple hash-based implementation with operators to iterate over the array.

# associative array handling using hashing                                   #

# the modes allowed as associative array element values - change to suit     #
MODE AAVALUE = STRING;
# the modes allowed as associative array element keys - change to suit       #
MODE AAKEY   = STRING;
# nil element value                                                          #
REF AAVALUE nil value = NIL;

# an element of an associative array                                         #
MODE AAELEMENT = STRUCT( AAKEY key, REF AAVALUE value );
# a list of associative array elements - the element values with a           #
# particular hash value are stored in an AAELEMENTLIST                       #
MODE AAELEMENTLIST = STRUCT( AAELEMENT element, REF AAELEMENTLIST next );
# nil element list reference                                                 #
REF AAELEMENTLIST nil element list = NIL;
# nil element reference                                                      #
REF AAELEMENT     nil element      = NIL;

# the hash modulus for the associative arrays                                #
INT hash modulus = 256;

# generates a hash value from an AAKEY - change to suit                      #
OP HASH = ( STRING key )INT:
BEGIN
    INT result := ABS ( UPB key - LWB key ) MOD hash modulus;
    FOR char pos FROM LWB key TO UPB key DO
        result PLUSAB ( ABS key[ char pos ] - ABS " " );
        result MODAB  hash modulus
    OD;
    result
END; # HASH #

# a mode representing an associative array                                    #
MODE AARRAY = STRUCT( [ 0 : hash modulus - 1 ]REF AAELEMENTLIST elements
                    , INT                                       curr hash
                    , REF AAELEMENTLIST                         curr position
                    );

# initialises an associative array so all the hash chains are empty           #
OP   INIT = ( REF AARRAY array )REF AARRAY:
     BEGIN
         FOR hash value FROM 0 TO hash modulus - 1 DO ( elements OF array )[ hash value ] := nil element list OD;
         array
     END; # INIT #

# gets a reference to the value corresponding to a particular key in an      #
# associative array - the element is created if it doesn't exist             #
PRIO // = 1;
OP   // = ( REF AARRAY array, AAKEY key )REF AAVALUE:
BEGIN
    REF AAVALUE result;
    INT         hash value = HASH key;
    # get the hash chain for the key #
    REF AAELEMENTLIST element := ( elements OF array )[ hash value ];
    # find the element in the list, if it is there #
    BOOL found element := FALSE;
    WHILE ( element ISNT nil element list )
      AND NOT found element
    DO
        found element := ( key OF element OF element = key );
        IF found element
        THEN
            result  := value OF element OF element
        ELSE
            element := next OF element
        FI
    OD;
    IF NOT found element
    THEN
        # the element is not in the list #
        # - add it to the front of the hash chain #
        ( elements OF array )[ hash value ]
                            := HEAP AAELEMENTLIST
                            := ( HEAP AAELEMENT := ( key
                                                   , HEAP AAVALUE := ""
                                                   )
                               , ( elements OF array )[ hash value ]
                               );
        result := value OF element OF ( elements OF array )[ hash value ]
    FI;
    result
END; # // #

# returns TRUE if array contains key, FALSE otherwise                        #
PRIO CONTAINSKEY = 1;
OP   CONTAINSKEY = ( REF AARRAY array, AAKEY key )BOOL:
BEGIN
    # get the hash chain for the key #
    REF AAELEMENTLIST element := ( elements OF array )[ HASH key ];
    # find the element in the list, if it is there #
    BOOL found element := FALSE;
    WHILE ( element ISNT nil element list )
      AND NOT found element
    DO
        found element := ( key OF element OF element = key );
        IF NOT found element
        THEN
            element := next OF element
        FI
    OD;
    found element
END; # CONTAINSKEY #

# gets the first element (key, value) from the array                         #
OP FIRST = ( REF AARRAY array )REF AAELEMENT:
BEGIN
    curr hash     OF array := LWB ( elements OF array ) - 1;
    curr position OF array := nil element list;
    NEXT array
END; # FIRST #

# gets the next element (key, value) from the array                          #
OP NEXT  = ( REF AARRAY array )REF AAELEMENT:
BEGIN
    WHILE ( curr position OF array IS nil element list )
      AND   curr hash     OF array < UPB ( elements OF array )
    DO
        # reached the end of the current element list - try the next         #
        curr hash     OF array +:= 1;
        curr position OF array  := ( elements OF array )[ curr hash OF array ]
    OD;
    IF   curr hash OF array > UPB ( elements OF array )
    THEN
        # no more elements #
        nil element
    ELIF curr position OF array IS nil element list
    THEN
        # reached the end of the table #
        nil element
    ELSE
        # have another element #
        REF AAELEMENTLIST found element = curr position OF array;
        curr position OF array := next OF curr position OF array;
        element OF found element
    FI
END; # NEXT #

# test the associative array #
BEGIN
    # create an array and add some values  #
    REF AARRAY a1 := INIT LOC AARRAY;
    a1 // "k1" := "k1 value";
    a1 // "z2" := "z2 value";
    a1 // "k1" := "new k1 value";
    a1 // "k2" := "k2 value";
    a1 // "2j" := "2j value";
    # iterate over the values #
    REF AAELEMENT e := FIRST a1;
    WHILE e ISNT nil element
    DO
        print( ( "  (" + key OF e + ")[" + value OF e + "]", newline ) );
        e := NEXT a1
    OD
END
Output:
  (2j)[2j value]
  (k1)[new k1 value]
  (k2)[k2 value]
  (z2)[z2 value]

App Inventor

Associative arrays in App Inventor are lists of key:value 'pairs'.
When a list is organized as pairs, the lookup in pairs block can be used to retrieve an associated value from a key name.
<VIEW BLOCKS AND ANDROID APP>

Arturo

; create a dictionary
d: #[
	name: 		"john"
	surname: 	"doe"
	age: 		34
]

; Iterate over key/value pairs
loop d [key,value][
	print ["key =" key ", value =" value]
]

print "----"

; Iterate over keys
loop keys d [k][
	print ["key =" k]
]

print "----"

; Iterate over values
loop values d [v][
	print ["value =" v]
]
Output:
key = name , value = john 
key = surname , value = doe 
key = age , value = 34 
----
key = name 
key = surname 
key = age 
----
value = john 
value = doe 
value = 34

ATS

See Associative_array/Creation#ATS.

AutoHotkey

Works with: AutoHotkey_L

From the documentation

; Create an associative array
obj := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
enum := obj._NewEnum()
While enum[key, value]
    t .= key "=" value "`n"
MsgBox % t

AWK

In AWK "arrays" are always associative arrays, and the only way to iterate over them is by keys (indexes in the AWK terminology), in undefined order.

BEGIN {
  a["hello"] = 1
  a["world"] = 2
  a["!"] = 3

  # iterate over keys, undefined order
  for(key in a) {
    print key, a[key]
  }
}

As AWK was often used in (Bourne) shell scripts, sorting was done by a pipe of two awk programs and the sort command. Today, 'gawk' allows to set the order of iteration:

BEGIN {
  a["hello"] = 1
  a["world"] = 2
  a["!"] = 3
  PROCINFO["sorted_in"] = "@ind_str_asc" # controls index order
  # iterate over keys, indices as strings sorted ascending
  for(key in a) {
    print key, a[key]
  }
}

Babel

In Babel, associative arrays are referred to as maps. To create a map from a list-of-lists:

births (('Washington' 1732) ('Lincoln' 1809) ('Roosevelt' 1882) ('Kennedy' 1917)) ls2map ! <

To iterate over a map, in the primary sense, use the overmap utility. We will copy the map (cp operator) so as not to modify the original:

births cp dup {1 +} overmap !

To see the results, use the valmap operator:

valmap ! lsnum !
Output:
( 1918 1733 1883 1810 )

There are many ways to interact with a map in Babel. Most of these begin by converting the map to a list or list-of-lists. To look up a list of specific values from the map, by key, use the lumapls utility:

births ('Roosevelt' 'Kennedy') lumapls ! lsnum !
Output:
( 1882 1917 )

To convert the entire map back to a list of key-value pairs:

births map2ls !

To view the list:

{give swap << " " << itod << "\n" <<} each
Output:
Kennedy 1917
Washington 1732
Roosevelt 1882
Lincoln 1809

To merge two maps together, use the mapmerge utility:

foo (("bar" 17) ("baz" 42)) ls2map ! <
births foo mergemap !

To view the results:

births map2ls ! {give swap << " " << itod << "\n" <<} each
Output:
baz 42
Kennedy 1917
bar 17
Washington 1732
Roosevelt 1882
Lincoln 1809

For more information on maps in Babel, view std.sp (see the section titled "map utilities").

BASIC

BaCon

DECLARE associative ASSOC STRING

associative("abc") = "first three"
associative("mn") = "middle two"
associative("xyz") = "last three"

LOOKUP associative TO keys$ SIZE amount
FOR i = 0 TO amount - 1
    PRINT keys$[i], ":", associative(keys$[i])
NEXT
Output:
prompt$ ./assoc
abc:first three
mn:middle two
xyz:last three

LOOKUP creates a numerically indexed array of the keys of the associative array, with the number of elements stored in the field following the SIZE keyword.

BASIC256

Solution is at Associative_array/Creation#BASIC256.

BBC BASIC

      REM Store some values with their keys:
      PROCputdict(mydict$, "FF0000", "red")
      PROCputdict(mydict$, "00FF00", "green")
      PROCputdict(mydict$, "0000FF", "blue")
      
      REM Iterate through the dictionary:
      i% = 1
      REPEAT
        i% = FNdict(mydict$, i%, v$, k$)
        PRINT v$, k$
      UNTIL i% = 0
      END
      
      DEF PROCputdict(RETURN dict$, value$, key$)
      IF dict$ = "" dict$ = CHR$(0)
      dict$ += key$ + CHR$(1) + value$ + CHR$(0)
      ENDPROC
      
      DEF FNdict(dict$, I%, RETURN value$, RETURN key$)
      LOCAL J%, K%
      J% = INSTR(dict$, CHR$(1), I%)
      K% = INSTR(dict$, CHR$(0), J%)
      value$ = MID$(dict$, I%+1, J%-I%-1)
      key$ = MID$(dict$, J%+1, K%-J%-1)
      IF K% >= LEN(dict$) THEN K% = 0
      = K%

Bracmat

(  new$hash:?myhash
& (myhash..insert)$(title."Some title")
& (myhash..insert)$(formula.a+b+x^7)
& (myhash..insert)$(fruit.apples oranges kiwis)
& (myhash..insert)$(meat.)
& (myhash..insert)$(fruit.melons bananas)
& (myhash..remove)$formula
& (myhash..insert)$(formula.x^2+y^2)
&   (myhash..forall)
  $ ( 
    =   key value
      .     whl
          ' ( !arg:(?key.?value) ?arg
            & put$("key:" !key "\nvalue:" !value \n)
            )
        & put$\n
    )
);
Output:
key: meat
value:

key: title
value: Some title

key: formula
value: x^2+y^2

key: fruit
value: melons bananas
key: fruit
value: apples oranges kiwis

Brat

h = [ hello: 1 world: 2 :! : 3]

#Iterate over key, value pairs
h.each { k, v |
  p "Key: #{k} Value: #{v}"
}

#Iterate over keys
h.each_key { k |
  p "Key: #{k}"
}

#Iterate over values
h.each_value { v |
  p "Value: #{v}"
}

C

Solution is at Associative arrays/Creation/C.

C#

using System;
using System.Collections.Generic;

namespace AssocArrays
{
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<string,int> assocArray = new Dictionary<string,int>();

            assocArray["Hello"] = 1;
            assocArray.Add("World", 2);
            assocArray["!"] = 3;

            foreach (KeyValuePair<string, int> kvp in assocArray)
            {
                Console.WriteLine(kvp.Key + " : " + kvp.Value);
            }

            foreach (string key in assocArray.Keys)
            {
                Console.WriteLine(key);
            }

            foreach (int val in assocArray.Values)
            {
                Console.WriteLine(val.ToString());
            }
        }
    }
}

C++

Works with: C++11
#include <iostream>
#include <map>
#include <string>

int main() {
  std::map<std::string, int> dict {
    {"One", 1},
    {"Two", 2},
    {"Three", 7}
  };

  dict["Three"] = 3;

  std::cout << "One: " << dict["One"] << std::endl;
  std::cout << "Key/Value pairs: " << std::endl;
  for(auto& kv: dict) {
    std::cout << "  " << kv.first << ": " << kv.second << std::endl;
  }

  return 0;
}


Pre C++11:

std::map<std::string, int> myDict;
myDict["hello"] = 1;
myDict["world"] = 2;
myDict["!"] = 3;

// iterating over key-value pairs:
for (std::map<std::string, int>::iterator it = myDict.begin(); it != myDict.end(); ++it) {
    // the thing pointed to by the iterator is an std::pair<const std::string, int>&
    const std::string& key = it->first;
    int& value = it->second;
    std::cout << "key = " << key << ", value = " << value << std::endl;
}

Ceylon

shared void run() {

	value myMap = map {
		"foo" -> 5,
		"bar" -> 10,
		"baz" -> 15
	};
	
	for(key in myMap.keys) {
		print(key);
	}
	
	for(item in myMap.items) {
		print(item);
	}
	
	for(key->item in myMap) {
		print("``key`` maps to ``item``");
	}
	
}

Chapel

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);
Output:
have key: O2
have key: NaCl
have key: H2O
have value: oxygen
have value: salt
have value: water
have element: O2 -> oxygen
have element: NaCl -> salt
have element: H2O -> water

Clojure

(doseq [[k v] {:a 1, :b 2, :c 3}]
  (println k "=" v))

(doseq [k  (keys {:a 1, :b 2, :c 3})]
  (println k))

(doseq [v  (vals {:a 1, :b 2, :c 3})]
  (println v))

CoffeeScript

hash =
  a: 'one'
  b: 'two'

for key, value of hash
  console.log key, value
  
for key of hash
  console.log key

Common Lisp

Common Lisp has three common idioms for associating keys with values: association lists (alists), property lists (plists), and hash tables.

With association lists (alists)

The association list is a list of conses, each of whose car is a key and whose cdr is a value. The standard mapping and print functions can be used to print key/value pairs, keys, and values.

;; iterate using dolist, destructure manually
(dolist (pair alist)
  (destructuring-bind (key . value) pair
    (format t "~&Key: ~a, Value: ~a." key value)))

;; iterate and destructure with loop
(loop for (key . value) in alist
      do (format t "~&Key: ~a, Value: ~a." key value))

With property lists (plists)

Property lists are lists of alternating keys and values, where each value's key is the element of the list immediately following it. Printing could be done with standard mapping functions, but loop's destructuring makes things a bit easier.

(loop for (key value) on plist :by 'cddr
      do (format t "~&Key: ~a, Value: ~a." key value))

With hash tables

Lisp also has built-in hash tables, and there are several ways to map over these. The first is maphash which takes a function of two arguments (the key and value) and the hash table.

(maphash (lambda (key value)
           (format t "~&Key: ~a, Value: ~a." key value))
         hash-table)

The loop construct also supports extracting key/value pairs from hash tables.

(loop for key being each hash-key of hash-table using (hash-value value)
      do (format t "~&Key: ~a, Value: ~a." key value))

There is also a macro with-hash-table-iterator which locally binds a name to produce associated keys and values of the hash table; while rarely used, it is the most powerful operation.

(with-hash-table-iterator (next-entry hash-table)
  (loop
   (multiple-value-bind (nextp key value) (next-entry)
     (if (not nextp)
       (return)
       (format t "~&Key: ~a, Value: ~a." key value)))))

Alternate solution

I use Allegro CL 10.1

;; Project : Associative array/Iteration

(setf x (make-array '(3 2)           
           :initial-contents '(("hello" 13 ) ("world" 31) ("!" 71))))
(setf xlen (array-dimensions x))
(setf len (car xlen))
(dotimes (n len)  
               (terpri)
               (format t "~a" (aref x n 0))
               (format t "~a" " : ") 
               (format t "~a" (aref x n 1)))

Output:

hello : 13
world : 31
! : 71

Crystal

dict = {'A' => 1, 'B' => 2}

dict.each { |pair|
  puts pair
}

dict.each_key { |key|
  puts key
}

dict.each_value { |value|
  puts value
}
Output:
{'A', 1}
{'B', 2}
A
B
1
2

D

Works with: D version 2
import std.stdio: writeln;

void main() {
    // the associative array
    auto aa = ["alice":2, "bob":97, "charlie":45];

    // how to iterate key/value pairs:
    foreach (key, value; aa)
        writeln("1) Got key ", key, " with value ", value);
    writeln();

    // how to iterate the keys:
    foreach (key, _; aa)
        writeln("2) Got key ", key);
    writeln();

    // how to iterate the values:
    foreach (value; aa)
        writeln("3) Got value ", value);
    writeln();

    // how to extract the values, lazy:
    foreach (value; aa.byValue())
        writeln("4) Got value ", value);
    writeln();

    // how to extract the keys, lazy:
    foreach (key; aa.byKey())
        writeln("5) Got key ", key);
    writeln();

    // how to extract all the keys:
    foreach (key; aa.keys)
        writeln("6) Got key ", key);
    writeln();

    // how to extract all the values:
    foreach (value; aa.values)
        writeln("7) Got value ", value);
}

Dao

dict = { 'def' => 1, 'abc' => 2 }

for( keyvalue in dict ) io.writeln( keyvalue );
for( key in dict.keys(); value in dict.values() ) io.writeln( key, value )
dict.iterate { [key, value]
    io.writeln( key, value )
}

Dart

main(){
	var fruits = {
		'apples':  'red',
		'oranges': 'orange',
		'bananas': 'yellow',
		'pears':   'green',
		'plums':   'purple'
	};
	
	print('Key Value pairs:');
	fruits.forEach( (fruits, color) => print( '$fruits are $color' ) );
	
	print('\nKeys only:');
	fruits.keys.forEach( ( key ) => print( key ) );
	
	print('\nValues only:');
	fruits.values.forEach( ( value ) => print( value ) );
}
Output:
Key Value pairs:
apples are red
oranges are orange
bananas are yellow
pears are green
plums are purple

Keys only:
apples
oranges
bananas
pears
plums

Values only:
red
orange
yellow
green
purple

Delphi

program AssociativeArrayIteration;

{$APPTYPE CONSOLE}

uses SysUtils, Generics.Collections;

var
  i: Integer;
  s: string;
  lDictionary: TDictionary<string, Integer>;
  lPair: TPair<string, Integer>;
begin
  lDictionary := TDictionary<string, Integer>.Create;
  try
    lDictionary.Add('foo', 5);
    lDictionary.Add('bar', 10);
    lDictionary.Add('baz', 15);
    lDictionary.AddOrSetValue('foo', 6);

    for lPair in lDictionary do
      Writeln(Format('Pair: %s = %d', [lPair.Key, lPair.Value]));
    for s in lDictionary.Keys do
      Writeln('Key: ' + s);
    for i in lDictionary.Values do
      Writeln('Value: ', i);
  finally
    lDictionary.Free;
  end;
end.

Dyalect

var t = (x: 1, y: 2, z: 3)

for x in t.Keys() {
    print("\(x)=\(t[x])")
}
Output:
x=1
y=2
z=3

E

In E, the basic iteration protocol and syntax work over key-value pairs. Therefore, any iteration over a map or other collection is always key-value, though the user may choose to ignore the keys or the values.

The for loop takes either one pattern, for the value, or two, for the key and value; for iterating over keys alone the value may be given an ignore-pattern (_).

def map := [
  "a" => 1,
  "b" => 2,
  "c" => 3,
]

for key => value in map {
  println(`$key $value`)
}

for value in map {     # ignore keys
  println(`. $value`)
}

for key => _ in map {  # ignore values
  println(`$key .`)
}

for key in map.domain() {     # iterate over the set whose values are the keys
  println(`$key .`)
}

EasyLang

# use array of array for this
clothing$[][] = [ [ "type" "t-shirt" ] [ "color" "red" ] [ "size" "xl" ] ]
for i to len clothing$[][]
   print clothing$[i][1] & ": " & clothing$[i][2]
.
Output:

=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(lib 'hash) ;; load hash.lib
(define H (make-hash))
;; fill hash table
(hash-set H 'Simon 42)
(hash-set H 'Albert 666)
(hash-set H 'Antoinette 33)

;; iterate over (key . value ) pairs
(for ([kv H]) (writeln kv))
(Simon . 42)    
(Albert . 666)    
(Antoinette . 33)    

;; iterate over keys
(for ([k (hash-keys H)]) (writeln 'key-> k))
key->     Simon    
key->     Albert    
key->     Antoinette    

;; iterate over values
(for ([v (hash-values H)]) (writeln 'value-> v))
value->     42    
value->     666    
value->     33    
</syntaxhighlight>

=={{header|Elena}}==
ELENA 5.0 :
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
import extensions;
 
public program()
{
    // 1. Create
    var map := Dictionary.new();
    map["key"] := "foox";
    map["key"] := "foo";
    map["key2"]:= "foo2";
    map["key3"]:= "foo3";
    map["key4"]:= "foo4";
 
    // Enumerate
    map.forEach:
        (keyValue){ console.printLine(keyValue.Key," : ",keyValue.Value) }
}</syntaxhighlight>

=== Strong typed dictionary ===
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
import extensions;

public program()
{
    // 1. Create
    auto map := new Map<string,string>();
    map["key"] := "foox";
    map["key"] := "foo";
    map["key2"]:= "foo2";
    map["key3"]:= "foo3";
    map["key4"]:= "foo4";

    // Enumerate
    map.forEach:
        (tuple){ console.printLine(tuple.Item1," : ",tuple.Item2) }
}</syntaxhighlight>

=={{header|Elixir}}==
<syntaxhighlight lang="elixir">IO.inspect d = Map.new([foo: 1, bar: 2, baz: 3])
Enum.each(d, fn kv -> IO.inspect kv end)
Enum.each(d, fn {k,v} -> IO.puts "#{inspect k} => #{v}" end)
Enum.each(Map.keys(d), fn key -> IO.inspect key end)
Enum.each(Map.values(d), fn value -> IO.inspect value end)</syntaxhighlight>

{{out}}
<pre>
%{bar: 2, baz: 3, foo: 1}
{:bar, 2}
{:baz, 3}
{:foo, 1}
:bar => 2
:baz => 3
:foo => 1
:bar
:baz
:foo
2
3
1

EMal

Map map = text%text["Italy" => "Rome", "France" => "Paris"]
map.insert("Germany", "Berlin")
map["Spain"] = "Madrid"
writeLine("== pairs ==")
for each Pair pair in map
  writeLine(pair)
end
writeLine("== keys ==")
for each text key in map.keys()
  writeLine(key)
end
writeLine("== values ==")
for each text value in map.values()
  writeLine(value)
end
Output:
== pairs ==
[Italy,Rome]
[France,Paris]
[Germany,Berlin]
[Spain,Madrid]
== keys ==
Italy
France
Germany
Spain
== values ==
Rome
Paris
Berlin
Madrid

Erlang

-module(assoc).
-compile([export_all]).

test_create() ->   
    D = dict:new(),
    D1 = dict:store(foo,1,D),
    D2 = dict:store(bar,2,D1),
    print_vals(D2).

print_vals(D) ->
    lists:foreach(fun (K) ->
                          io:format("~p: ~b~n",[K,dict:fetch(K,D)])
                  end, dict:fetch_keys(D)).
Output:
32> assoc:test_create().
bar: 2
foo: 1
ok

F#

Iterating over both.

let myMap = [ ("Hello", 1); ("World", 2); ("!", 3) ]

for k, v in myMap do
  printfn "%s -> %d" k v

Iterating over either keys or values only can be achieved through use of the _ wildcard token.

// Only prints the keys.
for k, _ in myMap do
    printfn "%s" k

// Only prints the values.
for _, v in myMap do
    printfn "%d" v

Factor

H{ { "hi" "there" } { "a" "b" } } [ ": " glue print ] assoc-each

assoc-each places both the key and the value on top of the data stack. A simple drop or nip enables iterating over only keys or values.

H{ { "hi" "there" } { "a" "b" } } [ drop print ] assoc-each ! print keys
H{ { "hi" "there" } { "a" "b" } } [ nip print ] assoc-each ! print values

There's also assoc-map, assoc-find, assoc-filter and many more.

Fantom

Given a map, each iterates over pairs of values-keys. keys and vals retrieve a list of keys or values, respectively.

class Main
{
  public static Void main ()
  {
    Int:Str map := [1:"alpha", 2:"beta", 3:"gamma"]

    map.keys.each |Int key|
    {
      echo ("Key is: $key")
    }

    map.vals.each |Str value|
    {
      echo ("Value is: $value")
    }

    map.each |Str value, Int key|
    {
      echo ("Key $key maps to $value")
    }
  }
}

Forth

include ffl/hct.fs
include ffl/hci.fs

\ Create hashtable and iterator in dictionary
10     hct-create htable
htable hci-create hiter

\ Insert entries
1 s" hello" htable hct-insert
2 s" world" htable hct-insert
3 s" !"     htable hct-insert

: iterate
  hiter hci-first
  BEGIN
  WHILE
    ." key = " hiter hci-key type ." , value = " . cr
    hiter hci-next
  REPEAT
;

iterate
\ Written in ANS-Forth; tested under VFX.
\ Requires the novice package: http://www.forth.org/novice.html
\ The following should already be done:
\ include novice.4th
\ include association.4th

\ I would define high-level languages as those that allow programs to be written without explicit iteration. Iteration is a major source of bugs.
\ The example from the FFL library doesn't hide iteration, whereas this example from the novice-package does.


marker AssociationIteration.4th

\ ******
\ ****** The following defines a node in an association (each node is derived from ELEMENT).
\ ******

element
    w field .inventor
constant language           \ describes a programming language
    
: init-language ( inventor name node -- node )
    init-element >r
    hstr r@ .inventor !
    r> ;
    
: new-language ( inventor name -- node )    
    language alloc
    init-language ;
    
: show-language ( count node -- )    
    >r
    1+                      \ -- count+1
    cr  r@ .key @ count colorless type  ." invented by: "  r@ .inventor @ count type
    rdrop ;
    
: show-languages-forward ( handle -- )
    0                       \ -- handle count
    swap .root @  ['] show-language  walk> 
    cr ." count: " . 
    cr ;
        
: show-languages-backward ( handle -- )
    0                       \ -- handle count
    swap .root @  ['] show-language  <walk
    cr ." count: " . 
    cr ;
        
: kill-language-attachments ( node -- )    
    dup .inventor @  dealloc
    kill-key ;
    
: copy-language-attachments ( src dst -- )    
    over .inventor @  hstr
    over .inventor !
    copy-key ;

        
\ ******
\ ****** The following defines the association itself (the handle).
\ ******

association
constant languages          \ describes a set of programming languages    

: init-languages ( record -- record )
    >r
    ['] compare  ['] kill-language-attachments  ['] copy-language-attachments
    r> init-association ;
    
: new-languages ( -- record )    
    languages alloc
    init-languages ;

        
\ ******
\ ****** The following filters one association into another, including everything that matches a particular inventor.
\ ******

: <filter-inventor> { inventor handle new-handle node -- inventor handle new-handle }
    inventor count  node .inventor @ count  compare  A=B = if
        node handle dup-element  new-handle insert  then
    inventor handle new-handle ;
    
: filter-inventor ( inventor handle -- new-handle )    
    dup similar-association                             \ -- inventor handle new-handle 
    over .root @  ['] <filter-inventor>  walk>          \ -- inventor handle new-handle
    nip nip ;

\ ******
\ ****** The following is a demonstration with some sample data.
\ ******

    
new-languages 
    c" Moore, Chuck"                c" Forth     "      new-language  over insert
    c" Ichiah, Jean"                c" Ada       "      new-language  over insert
    c" Wirth, Niklaus"              c" Pascal    "      new-language  over insert
    c" Wirth, Niklaus"              c" Oberon    "      new-language  over insert
    c" McCarthy, John"              c" Lisp      "      new-language  over insert
    c" van Rossum, Guido"           c" Python    "      new-language  over insert
    c" Gosling, Jim"                c" Java      "      new-language  over insert
    c" Ierusalimschy, Roberto"      c" Lua       "      new-language  over insert
    c" Matsumoto, Yukihiro"         c" Ruby      "      new-language  over insert
    c" Pestov, Slava"               c" Factor    "      new-language  over insert
    c" Gosling, James"              c" Java      "      new-language  over insert
    c" Wirth, Niklaus"              c" Modula-2  "      new-language  over insert
    c" Ritchie, Dennis"             c" C         "      new-language  over insert
    c" Stroustrup, Bjarne"          c" C++       "      new-language  over insert
constant some-languages    

    
cr .( everything in SOME-LANGUAGES ordered forward: )
    
some-languages show-languages-forward


cr .( everything in SOME-LANGUAGES ordered backward: )
    
some-languages show-languages-backward


cr .( everything in SOME-LANGUAGES invented by Wirth: )

c" Wirth, Niklaus" some-languages filter-inventor           dup show-languages-forward  kill-association


cr .( everything in SOME-LANGUAGES within 'F' and 'L': )

c" F"  c" L"  some-languages  filter within                 dup show-languages-forward  kill-association

    
cr .( everything in SOME-LANGUAGES not within 'F' and 'L': )

c" F"  c" L"  some-languages  filter without                dup show-languages-forward  kill-association


some-languages kill-association
Output:
everything in SOME-LANGUAGES ordered forward: 
Ada       invented by: Ichiah, Jean
C         invented by: Ritchie, Dennis
C++       invented by: Stroustrup, Bjarne
Factor    invented by: Pestov, Slava
Forth     invented by: Moore, Chuck
Java      invented by: Gosling, James
Lisp      invented by: McCarthy, John
Lua       invented by: Ierusalimschy, Roberto
Modula-2  invented by: Wirth, Niklaus
Oberon    invented by: Wirth, Niklaus
Pascal    invented by: Wirth, Niklaus
Python    invented by: van Rossum, Guido
Ruby      invented by: Matsumoto, Yukihiro
count: 13 

everything in SOME-LANGUAGES ordered backward: 
Ruby      invented by: Matsumoto, Yukihiro
Python    invented by: van Rossum, Guido
Pascal    invented by: Wirth, Niklaus
Oberon    invented by: Wirth, Niklaus
Modula-2  invented by: Wirth, Niklaus
Lua       invented by: Ierusalimschy, Roberto
Lisp      invented by: McCarthy, John
Java      invented by: Gosling, James
Forth     invented by: Moore, Chuck
Factor    invented by: Pestov, Slava
C++       invented by: Stroustrup, Bjarne
C         invented by: Ritchie, Dennis
Ada       invented by: Ichiah, Jean
count: 13 

everything in SOME-LANGUAGES invented by Wirth: 
Modula-2  invented by: Wirth, Niklaus
Oberon    invented by: Wirth, Niklaus
Pascal    invented by: Wirth, Niklaus
count: 3 

everything in SOME-LANGUAGES within 'F' and 'L': 
Factor    invented by: Pestov, Slava
Forth     invented by: Moore, Chuck
Java      invented by: Gosling, James
count: 3 

everything in SOME-LANGUAGES not within 'F' and 'L': 
Ada       invented by: Ichiah, Jean
C         invented by: Ritchie, Dennis
C++       invented by: Stroustrup, Bjarne
Lisp      invented by: McCarthy, John
Lua       invented by: Ierusalimschy, Roberto
Modula-2  invented by: Wirth, Niklaus
Oberon    invented by: Wirth, Niklaus
Pascal    invented by: Wirth, Niklaus
Python    invented by: van Rossum, Guido
Ruby      invented by: Matsumoto, Yukihiro
count: 10 

FreeBASIC

Use the typedefs and data from Associative Array/Creation#FreeBASIC as an include.

Since this data structure stores the keys and values together it makes little sense to iterate through the same array three times to print different parts of it, hence I will only print the key:value pairs.

#include"assoc.bas"

function get_dict_data_string( d as dicitem ) as string
    select case d.datatype
        case BOOL
            if d.value.bool then return "true" else return "false"
        case INTEG
            return str(d.value.integ)
        case STRNG
            return """"+d.value.strng+""""
        case FLOAT
            return str(d.value.float)
        case BYYTE
            return str(d.value.byyte)
        case else
            return "DATATYPE ERROR"
    end select
end function

sub print_keyval_pair( d as dicentry )
    print using "{&} : {&}";get_dict_data_string( d.key ); get_dict_data_string(d.value)
    
end sub

for i as uinteger = 0 to ubound(Dictionary)
    print_keyval_pair(Dictionary(i))
next i
Output:
{"Cat"} : {"Mittens"}
{32767} : {2.718281828}

Free Pascal

FPC 3.2.0+. Similar to Delphi:

program AssociativeArrayIteration;
{$mode delphi}{$ifdef windows}{$apptype console}{$endif}
uses Generics.Collections;

type 
  TlDictionary =  TDictionary<string, Integer>;
  TlPair = TPair<string,integer>;

var
  i: Integer;
  s: string;
  lDictionary: TlDictionary;
  lPair: TlPair;
begin
  lDictionary := TlDictionary.Create;
  try
    lDictionary.Add('foo', 5);
    lDictionary.Add('bar', 10);
    lDictionary.Add('baz', 15);
    lDictionary.AddOrSetValue('foo',6);
    for lPair in lDictionary do
      Writeln('Pair: ',Lpair.Key,' = ',lPair.Value);
    for s in lDictionary.Keys do
      Writeln('Key: ' + s);
    for i in lDictionary.Values do
      Writeln('Value: ', i);
  finally
    lDictionary.Free;
  end;
end.
Pair: foo = 6
Pair: bar = 10
Pair: baz = 15
Key: foo
Key: bar
Key: baz
Value: 6
Value: 10
Value: 15

Frink

d = new dict[[[1, "one"], [2, "two"]]]
for [key, value] = d
   println["$key\t$value"]

println[]
for key = keys[d]
   println["$key"]
Output:
2	two
1	one

2
1

FutureBasic

There are many ways to iterate over an associative array (dictionary) in FutureBasic. Below are a selection.

1. for ... in ...

void local fn DoIt
  CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
  CFStringRef key
  
  for key in dict
    print key, dict[key]
  next
end fn

fn DoIt

HandleEvents

2. Enumerator callback

void local fn MyDictEnumerator( dict as CFDictionaryRef, key as CFTypeRef, obj as CFTypeRef, stp as ^BOOL, userData as ptr )
  print key, obj
end fn

void local fn DoIt
  CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
  DictionaryEnumerateKeysAndObjects( dict, @fn MyDictEnumerator, NULL )
end fn

fn DoIt

HandleEvents

3. Array of keys

void local fn DoIt
  CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
  CFArrayRef keys = fn DictionaryAllKeys( dict )
  CFStringRef key
  
  for key in keys
    print key, dict[key]
  next
end fn

fn DoIt

HandleEvents

4. Array of values

void local fn DoIt
  CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
  CFArrayRef values = fn DictionaryAllValues( dict )
  CFStringRef value
  
  for value in values
    print value
  next
end fn

fn DoIt

HandleEvents

5. Key/object enumerators

void local fn DoIt
  CFDictionaryRef dict = @{@"A":@"Alpha", @"B":@"Bravo", @"C":@"Charlie", @"D":@"Delta"}
  CFStringRef     key
  CFTypeRef       obj
  
  EnumeratorRef keyEnumerator = fn DictionaryKeyEnumerator( dict )
  key = fn EnumeratorNextObject( keyEnumerator )
  while ( key )
    print key,dict[key]
    key = fn EnumeratorNextObject( keyEnumerator )
  wend
  
  print
  
  EnumeratorRef objectEnumerator = fn DictionaryObjectEnumerator( dict )
  obj = fn EnumeratorNextObject( objectEnumerator )
  while ( obj )
    print obj
    obj = fn EnumeratorNextObject( objectEnumerator )
  wend
end fn

fn DoIt

HandleEvents

Gambas

Click this link to run this code

Public Sub Main()
Dim cList As Collection = ["2": "quick", "4": "fox", "1": "The", "9": "dog", "7": "the", "5": "jumped", "3": "brown", "6": "over", "8": "lazy"]
Dim siCount As Short
Dim sTemp As String 

For Each sTemp In cList
  Print cList.key & "=" & sTemp;;
Next

Print

For siCount = 1 To cList.Count
  Print cList[Str(siCount)];;
Next

End

Output:

2=quick 4=fox 1=The 9=dog 7=the 5=jumped 3=brown 6=over 8=lazy 
The quick brown fox jumped over the lazy dog

Go

Language:

myMap := map[string]int {
	   "hello": 13,
	   "world": 31,
	   "!"    : 71 }

// iterating over key-value pairs:
for key, value := range myMap {
    fmt.Printf("key = %s, value = %d\n", key, value)
}

// iterating over keys:
for key := range myMap {
    fmt.Printf("key = %s\n", key)
}

// iterating over values:
for _, value := range myMap {
    fmt.Printf("value = %d\n", value)
}

Standard library templates:

In addition to the for/range features of the language, the text/template and html/template packages of the standard library have map iteration features. Some differences worth noting:

  • A single assigned value in a template is the map value. With the language for/range it is the key.
  • Templates have no equivalent of _; a dummy variable must be used.
  • In a template, if map keys are a comparable basic type, then iteration proceeds in key order. With the language for/range, iteration is in non-deterministic order.
package main

import (
    "os"
    "text/template"
)

func main() {
    m := map[string]int{
        "hello": 13,
        "world": 31,
        "!":     71,
    }

    // iterating over key-value pairs:
    template.Must(template.New("").Parse(`
{{- range $k, $v := . -}}
key = {{$k}}, value = {{$v}}
{{end -}}
`)).Execute(os.Stdout, m)

    // iterating over keys:
    template.Must(template.New("").Parse(`
{{- range $k, $v := . -}}
key = {{$k}}
{{end -}}
`)).Execute(os.Stdout, m)

    // iterating over values:
    template.Must(template.New("").Parse(`
{{- range . -}}
value = {{.}}
{{end -}}
`)).Execute(os.Stdout, m)
}
Output:

Note order by key.

key = !, value = 71
key = hello, value = 13
key = world, value = 31
key = !
key = hello
key = world
value = 71
value = 13
value = 31

Groovy

Solution:

def map = [lastName: "Anderson", firstName: "Thomas", nickname: "Neo", age: 24, address: "everywhere"] 

println "Entries:"
map.each { println it }

println()
println "Keys:"
map.keySet().each { println it }

println()
println "Values:"
map.values().each { println it }
Output:
Entries:
lastName=Anderson
firstName=Thomas
nickname=Neo
age=24
address=everywhere

Keys:
lastName
firstName
nickname
age
address

Values:
Anderson
Thomas
Neo
24
everywhere

Harbour

LOCAL arr := { 6 => 16, "eight" => 8, "eleven" => 11 }
LOCAL x

FOR EACH x IN arr
   // key, value
   ? x:__enumKey(), x
   // or key only
   ? x:__enumKey()
   // or value only
   ? x
NEXT

Haskell

with Data.Map:

import qualified Data.Map as M

myMap :: M.Map String Int
myMap = M.fromList [("hello", 13), ("world", 31), ("!", 71)]

main :: IO ()
main =
  (putStrLn . unlines) $
  [ show . M.toList     -- Pairs
  , show . M.keys       -- Keys
  , show . M.elems      -- Values
  ] <*>
  pure myMap
Output:
[("!",71),("hello",13),("world",31)]
["!","hello","world"]
[71,13,31]

Icon and Unicon

procedure main()
    t := table()
    every t[a := !"ABCDE"] := map(a)

    every pair := !sort(t) do
        write("\t",pair[1]," -> ",pair[2])

    writes("Keys:")
    every writes(" ",key(t))
    write()

    writes("Values:")
    every writes(" ",!t)
    write()
end
Output:
->aai
        A -> a
        B -> b
        C -> c
        D -> d
        E -> e
Keys: C E B D A
Values: c e b d a

Io

myDict := Map with(
    "hello", 13,
    "world", 31,
    "!"    , 71
)

// iterating over key-value pairs:
myDict foreach( key, value,
    writeln("key = ", key, ", value = ", value)
)
 
// iterating over keys:
myDict keys foreach( key,
    writeln("key = ", key)
)

// iterating over values:
myDict foreach( value,
    writeln("value = ", value)
)
// or alternatively:
myDict values foreach( value,
    writeln("value = ", value)
)

J

Note that all J operations either iterate over the items of an array or can be made to do so. So to iterate over some sequence you need to refer to that sequence.

Using the J example from Creating an Associative Array...

Keys

nl__example 0

Values

get__example each nl__example 0

Both keys and values

(,&< get__example) each nl__example 0

Note that this last is not likely to be useful in any practical context outside of learning the language.

Jakt

fn main() {
    let dictionary = ["foo": 1, "bar": 2]
    for entry in dictionary {
        // To get values, use
        // let value = entry.1
        println("{}", entry)
    }

    // Just keys
    for key in dictionary.keys() {
        println("{}", key)
    }
}
Output:
("bar", 2)
("foo", 1)
bar
foo

Java

See also, Java - Associative array/Creation.

You can access the key and value pairs by using the Map.entrySet method, which will return a Map.Entry.
It's worth noting that a Map.Entry also has the setValue method.

for (Map.Entry<String, Integer> entry : map.entrySet())
    System.out.println(entry);

You can access just the keys by using the Map.keySet method, which will return a Set.

for (String key : map.keySet())
    System.out.println(key);

And you can access just the values by using the Map.values method, which will return a Collection.

for (int value : map.values())
    System.out.println(value);


Java 8 version

Map<String, Integer> map = new HashMap<>();
map.put("hello", 1);
map.put("world", 2);
map.put("!", 3);

// iterating over key-value pairs:
map.forEach((k, v) -> {
    System.out.printf("key = %s, value = %s%n", k, v);
});

// iterating over keys:
map.keySet().forEach(k -> System.out.printf("key = %s%n", k));

// iterating over values:
map.values().forEach(v -> System.out.printf("value = %s%n", v));
Output:
key = !, value = 3
key = world, value = 2
key = hello, value = 1
key = !
key = world
key = hello
value = 3
value = 2
value = 1

JavaScript

JavaScript does not have associative arrays until ECMAScript 6 brings Maps. In versions up to ES5.1, you may add properties to an empty object to achieve the same effect.

var myhash = {}; //a new, empty object
myhash["hello"] = 3;
myhash.world = 6; //obj.name is equivalent to obj["name"] for certain values of name
myhash["!"] = 9;

//iterate using for..in loop
for (var key in myhash) {
  //ensure key is in object and not in prototype
  if (myhash.hasOwnProperty(key)) {
    console.log("Key is: " + key + '. Value is: ' + myhash[key]);
  }
}

//iterate using ES5.1 Object.keys() and Array.prototype.Map()
var keys = Object.keys(); //get Array of object keys (doesn't get prototype keys)
keys.map(function (key) {
  console.log("Key is: " + key + '. Value is: ' + myhash[key]);
});

Jq

In jq, there are several ways to iterate over compound structures:

- functionally, e.g. using map on an array
- by enumeration, i.e. by generating a stream
- by performing a reduction

For the sake of brevity, therefore, in the following we will only illustrate the enumerative approach.

With respect to associative arrays (i.e. JSON objects), the fundamental functions are:

- keys -- for producing an array of the keys (sorted) 
- .[]  -- for producing a stream of the values

In jq > 1.4, keys_unsorted, for producing an array of the keys (in the order of creation), is also available.

def mydict: {"hello":13, "world": 31, "!": 71};

# Iterating over the keys
mydict | keys[] 
# "!"
# "hello"
# "world"

# Iterating over the values:
mydict[]
# 13
# 31
# 71

# Generating a stream of {"key": key, "value": value} objects:
mydict | to_entries[]
# {"key":"hello","value":13}
# {"key":"world","value":31}
# {"key":"!","value":71}

# Generating a stream of [key,value] arrays:
mydict | . as $o | keys[] | [., $o[.]]
#["!",71]
#["hello",13]
#["world",31]

# Generating a stream of [key,value] arrays, without sorting (jq > 1.4 required)
mydict | . as $o | keys_unsorted[] | [., $o[.]]
# ["hello",13]
# ["world",31]
# ["!",71]

Julia

Works with: Julia version 0.6
dict = Dict("hello" => 13, "world" => 31, "!" => 71)

# applying a function to key-value pairs:
foreach(println, dict)

# iterating over key-value pairs:
for (key, value) in dict
    println("dict[$key] = $value")
end

# iterating over keys:
for key in keys(dict)
    @show key
end

# iterating over values:
for value in values(dict)
    @show value
end
Output:
key = !, value = 71
key = hello, value = 13
key = world, value = 31
key = !
key = hello
key = world
value = 71
value = 13
value = 31

K

Creating a dictionary.

   d: .((`"hello";1); (`"world";2);(`"!";3))

The keys are available via "!".

   !d
`hello `world `"!"

   $!d  / convert keys (symbols) as strings
("hello"
 "world"
 ,"!")

Print the key value pairs.

   `0:{,/$x,": ",d[x]}'!d
hello: 1
world: 2
!: 3

The values are available via "[]".

   d[]
1 2 3

  {x+1}'d[]
2 3 4

Kotlin

fun main() {
    val map = mapOf("hello" to 1, "world" to 2, "!" to 3)

    with(map) {
        forEach { println("key = ${it.key}, value = ${it.value}") }
        keys.forEach { println("key = $it") }
        values.forEach { println("value = $it") }
    }
}
Output:
key = hello, value = 1
key = world, value = 2
key = !, value = 3
key = hello
key = world
key = !
value = 1
value = 2
value = 3

Lang5

: first  0 extract nip ; : second  1 extract nip ; : nip  swap drop ;
: say(*)  dup first " => " 2 compress "" join . second . ;

[['foo 5] ['bar 10] ['baz 20]] 'say apply drop

Lasso

//iterate over associative array
//Lasso maps
	local('aMap' = map('weight' = 112, 
					'height' = 45,
					'name' = 'jason'))
	' Map output: \n  '
	#aMap->forEachPair => {^
		//display pair, then show accessing key and value individually
		#1+'\n  '
		#1->first+': '+#1->second+'\n  '
	^}
	//display keys and values separately
	'\n'
	' Map Keys: '+#aMap->keys->join(',')+'\n'
	' Map values: '+#aMap->values->join(',')+'\n'
	
	//display using forEach
	'\n'
	' Use ForEach to iterate Map keys: \n'
	#aMap->keys->forEach => {^
		#1+'\n'	
	^}
	'\n'
	' Use ForEach to iterate Map values: \n'
	#aMap->values->forEach => {^
		#1+'\n'	
	^}
	//the {^ ^} indicates that output should be printed (AutoCollect) , 
	// if output is not desired, just { } is used

LFE

Keys and Values

(let ((data '(#(key1 "foo") #(key2 "bar")))
      (hash (: dict from_list data)))
  (: dict fold 
    (lambda (key val accum) 
      (: io format '"~s: ~s~n" (list key val)))
    0
    hash))

Just Keys

(let ((data '(#(key1 "foo") #(key2 "bar")))
      (hash (: dict from_list data)))
  (: lists map 
    (lambda (key) 
      (: io format '"~s~n" (list key))) 
    (: dict fetch_keys hash)))

Liberty BASIC

Needs the sublist library from http://basic.wikispaces.com/SubList+Library since LB does not have built-in associative arrays.

data "red",      "255 50 50",       "green", "50 255 50",     "blue", "50 50 255"
data "my fave",  "220 120 120",     "black", "0 0 0"

myAssocList$ =""

for i =1 to 5
    read k$
    read dat$
    call sl.Set myAssocList$, k$, dat$
next i

keys$ = ""   ' List to hold the keys in myList$.
keys  = 0

keys = sl.Keys( myAssocList$, keys$)
print " Number of key-data pairs ="; keys

For i = 1 To keys
    keyName$ = sl.Get$( keys$, Str$( i))
    Print "  Key "; i; ":", keyName$, "Data: ", sl.Get$( myAssocList$, keyName$)
Next i

end
 Number of key-data pairs =5
 Key 1:      red           Data:         255 50 50
 Key 2:      green         Data:         50 255 50
 Key 3:      blue          Data:         50 50 255
 Key 4:      my fave       Data:         220 120 120
 Key 5:      black         Data:         0 0 0

Lingo

hash = [#key1:"value1", #key2:"value2", #key3:"value3"]

-- iterate over key-value pairs
repeat with i = 1 to hash.count
  put hash.getPropAt(i) & "=" & hash[i]
end repeat

-- iterating over values only can be written shorter
repeat with val in hash
  put val
end repeat

LiveCode

put 3 into fruit["apples"]
put 5 into fruit["pears"]
put 6 into fruit["oranges"]
put "none" into fruit["bananas"]

put "Keys:" & cr & the keys of fruit & cr into tTmp
put "Values 1:" & tab after tTmp
repeat for each line tKey in the keys of fruit
    put fruit[tkey] & comma after tTmp
end repeat

-- need to copy array as combine will change variable
put fruit into fruit2
combine fruit2 using comma
put cr & "Values2:" & tab after tTmp
repeat for each item f2val in fruit2
    put f2val & comma after tTmp
end repeat

combine fruit using return and ":"
put cr & "Key:Values" & cr & fruit after tTmp
-- alternatively, use same loop as for values 1 with tkey && fruit[tKey]

put tTmp

Output

Keys:
apples
pears
oranges
bananas
Values 1:	3,5,6,none,
Values2:	3,none,6,5,
Key:Values
apples:3
bananas:none
oranges:6
pears:5

Lua

local t = {
    ["foo"] = "bar",
    ["baz"] = 6,
    fortytwo = 7
}

for key,val in pairs(t) do
    print(string.format("%s: %s", key, val))
end
Output:
    fortytwo: 7
    foo: bar
    baz: 6

Note: the order in which pairs iterates over non-integer keys is not defined, so the order of lines in the output of the above code may differ from one run to another.

M2000 Interpreter

Module checkit {
      \\ Inventories are objects with keys and values, or keys (used as read only values)
      \\ They use hash function.
      \\ Function TwoKeys return Inventory object (as a pointer to object)
      Function TwoKeys {
            Inventory Alfa="key1":=100, "key2":=200
            =Alfa
      }
      M=TwoKeys()
      Print Type$(M)="Inventory"
      \\ Normal Use:
            \\ Inventories Keys are case sensitive
            \\ M2000 identifiers are not case sensitive
      Print M("key1"), m("key2")
      \\ numeric values can convert to strings
      Print M$("key1"), m$("key2")
      \\ Iteration
      N=Each(M)
      While N {
            Print Eval(N)  ' prints 100, 200 as number
            Print M(N^!)  ' The same using index N^
      }
      N=Each(M)
      While N {
            Print Eval$(N)  ' prints  100, 200 as strings
            Print M$(N^!)  ' The same using index N^
      }
      N=Each(M)
      While N {
            Print Eval$(N, N^)  ' Prints Keys
      }
      \\ double iteration
      Append M, "key3":=500
      N=Each(M, 1, -1)  ' start to end
      N1=Each(M, -1, 1) ' end to start
      \\ 3x3 prints
      While N {
            While N1 {
                  Print format$("{0}*{1}={2}", Eval(N1), Eval(N), Eval(N1)*Eval(N))
            }
      }
      \\ sort results from lower product to greater product (3+2+1, 6 prints only)
      N=Each(M, 1, -1)  
      While N {
            N1=Each(M, N^+1, -1) 
            While N1 {
                  Print format$("{0}*{1}={2}", Eval(N1), Eval(N), Eval(N1)*Eval(N))
            }
      }
      N=Each(M)
      N1=Each(M,-2, 1)  ' from second from end to start
      \\ print only 2 values. While block ends when one iterator finish
      While N, N1 {
            Print Eval(N1)*Eval(N)
      }         
}
Checkit

M4

divert(-1)
define(`for',
   `ifelse($#,0,``$0'',
   `ifelse(eval($2<=$3),1,
   `pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')
define(`new',`define(`$1[size]key',0)')
define(`asize',`defn(`$1[size]key')')
define(`aget',`defn(`$1[$2]')')
define(`akget',`defn(`$1[$2]key')')
define(`avget',`aget($1,akget($1,$2))')
define(`aset',
   `ifdef($1[$2],
      `',
      `define(`$1[size]key',incr(asize(`$1')))`'define($1[asize(`$1')]key,$2)')`'define($1[$2],$3)')
define(`dquote', ``$@'')
define(`akeyvalue',`dquote(akget($1,$2),aget($1,akget($1,$2)))')
define(`akey',`dquote(akget($1,$2))')
define(`avalue',`dquote(aget($1,akget($1,$2)))')
divert
new(`a')
aset(`a',`wow',5)
aset(`a',`wow',flame)
aset(`a',`bow',7)
key-value pairs
for(`x',1,asize(`a'),
   `akeyvalue(`a',x)
')
keys
for(`x',1,asize(`a'),
   `akey(`a',x)
')
values
for(`x',1,asize(`a'),
   `avalue(`a',x)
')
Output:
key-value pairs
`wow',`flame'
`bow',`7'

keys
`wow'
`bow'

values
`flame'
`7'

Maple

Iterate through indices when indices are all simple expressions:

> T := table( [ "A" = 1, "B" = 2, "C" = 3, "D" = 4 ] );
> for i in indices( T, nolist ) do print(i ) end:  
                                  "A"

                                  "B"

                                  "C"

                                  "D"

Iterate through indices when indices may be expression sequences:

> T := table( [ "a" = 1, "b" = 2, ("c","d") = 3 ] ):        
> for i in indices( T ) do print( i, T[ op( i ) ] ) end:    
                                ["a"], 1

                                ["b"], 2

                             ["c", "d"], 3

Return all index / entry pairs as equations:

> for i in indices( T, pairs ) do print( i) end:       
                                "a" = 1

                                "b" = 2

                             ("c", "d") = 3
> for i in entries( T ) do print( i) end:       
                                  [1]

                                  [3]

                                  [2]

Mathematica / Wolfram Language

keys=DownValues[#,Sort->False][[All,1,1,1]]&;
hashes=#/@keys[#]&;

a[2]="string";a["sometext"]=23;
keys[a]
->{2,sometext}
hashes[a]
->{string,23}

MATLAB / Octave

Associative arrays can be defined as structs in Matlab and Octave.

   keys = fieldnames(hash);
   for k=1:length(keys), 
        key = keys{k};
	value = getfield(hash,key);        % get value of key
	hash = setfield(hash,key,-value);  % set value of key
   end;

or

   keys = fieldnames(hash);
   for k=1:length(keys), 
        key = keys{k};
        value = hash.(key);     % get value of key
        hash.(key) = -value;    % set value of key
   end;

Maxima

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))$

MiniScript

d = { 3: "test", "foo": 3 }

for keyVal in d
    print keyVal   // produces results like: { "key": 3, "value": "test" }
end for

for key in d.indexes
    print key
end for

for  val in d.values
    print val
end for

NetRexx

/* NetRexx */
options replace format comments java crossref symbols

surname = 'Unknown' -- default value
surname['Fred'] = 'Bloggs'
surname['Davy'] = 'Jones'

try = 'Fred'
say surname[try] surname['Bert']

-- extract the keys
loop fn over surname
  say fn.right(10) ':' surname[fn]
  end fn

NewLISP

;; using an association list:
(setq alist '(("A" "a") ("B" "b") ("C" "c")))

;; list keys
(map first alist)

;; list values
(map last alist)

;; loop over the assocation list:
(dolist (elem alist)
  (println (format "%s -> %s" (first elem) (last elem))))

Nim

import tables

var t: Table[int,string]

t[1] = "one"
t[2] = "two"
t[3] = "three"
t[4] = "four"

echo "t has " & $t.len & " elements"

echo "has t key 4? " & $t.hasKey(4) 
echo "has t key 5? " & $t.hasKey(5)

#iterate keys
echo "key iteration:"
for k in t.keys:
  echo "at[" & $k & "]=" & t[k]

#iterate pairs
echo "pair iteration:"
for k,v in t.pairs:
  echo "at[" & $k & "]=" & v
Output:
t has 4 elements
has t key 4? true
has t key 5? false
key iteration:
at[1]=one
at[2]=two
at[3]=three
at[4]=four
pair iteration:
at[1]=one
at[2]=two
at[3]=three
at[4]=four

Oberon-2

Works with: oo2c Version 2
MODULE AssociativeArray;
IMPORT
  ADT:Dictionary,
  Object:Boxed,
  Out;
TYPE
  Key = STRING;
  Value = Boxed.LongInt;
  
VAR
  assocArray: Dictionary.Dictionary(Key,Value);
  iterK: Dictionary.IterKeys(Key,Value);
  iterV: Dictionary.IterValues(Key,Value);
  aux: Value;
  k: Key;
  
BEGIN
  assocArray := NEW(Dictionary.Dictionary(Key,Value));
  assocArray.Set("ten",NEW(Value,10));
  assocArray.Set("eleven",NEW(Value,11));
  
  aux := assocArray.Get("ten");
  Out.LongInt(aux.value,0);Out.Ln;
  aux := assocArray.Get("eleven");
  Out.LongInt(aux.value,0);Out.Ln;Out.Ln;
  
  (* Iterate keys *)
  iterK := assocArray.IterKeys();
  WHILE (iterK.Next(k)) DO
    Out.Object(k);Out.Ln
  END;
  
  Out.Ln;
  
  (* Iterate values *)
  iterV := assocArray.IterValues();
  WHILE (iterV.Next(aux)) DO
    Out.LongInt(aux.value,0);Out.Ln
  END
  
END AssociativeArray.

Objeck

class Iteration {
  function : Main(args : String[]) ~ Nil {
    assoc_array := Collection.StringMap->New();
    assoc_array->Insert("Hello", IntHolder->New(1));
    assoc_array->Insert("World", IntHolder->New(2));
    assoc_array->Insert("!", IntHolder->New(3));

    keys := assoc_array->GetKeys();
    values := assoc_array->GetValues();

    each(i : keys) {
      key := keys->Get(i)->As(String);
      value := assoc_array->Find(key)->As(IntHolder)->Get();
      "key={$key}, value={$value}"->PrintLine();
    };

    "-------------"->PrintLine();

    each(i : keys) {
      key := keys->Get(i)->As(String);
      value := values->Get(i)->As(IntHolder)->Get();
      "key={$key}, value={$value}"->PrintLine();
    };
  }
}

Objective-C

Works with: Objective-C version 2.0+
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt:13], @"hello",
                        [NSNumber numberWithInt:31], @"world",
                        [NSNumber numberWithInt:71], @"!", nil];

// iterating over keys:
for (id key in myDict) {
    NSLog(@"key = %@", key);
}

// iterating over values:
for (id value in [myDict objectEnumerator]) {
    NSLog(@"value = %@", value);
}
Works with: Objective-C version <2.0
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt:13], @"hello",
                        [NSNumber numberWithInt:31], @"world",
                        [NSNumber numberWithInt:71], @"!", nil];

// iterating over keys:
NSEnumerator *enm = [myDict keyEnumerator];
id key;
while ((key = [enm nextObject])) {
    NSLog(@"key = %@", key);
}

// iterating over values:
enm = [myDict objectEnumerator];
id value;
while ((value = [enm nextObject])) {
    NSLog(@"value = %@", value);
}
Works with: Cocoa version Mac OS X 10.6+
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt:13], @"hello",
                        [NSNumber numberWithInt:31], @"world",
                        [NSNumber numberWithInt:71], @"!", nil];

// iterating over keys and values:
[myDict enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
    NSLog(@"key = %@, value = %@", key, value);
}];

OCaml

Association array:

#!/usr/bin/env ocaml

let map = [| ('A', 1); ('B', 2); ('C', 3) |] ;;

(* iterate over pairs *)
Array.iter (fun (k,v) -> Printf.printf "key: %c - value: %d\n" k v) map ;;

(* iterate over keys *)
Array.iter (fun (k,_) -> Printf.printf "key: %c\n" k) map ;;

(* iterate over values *)
Array.iter (fun (_,v) -> Printf.printf "value: %d\n" v) map ;;

(* in functional programming it is often more useful to fold over the elements *)
Array.fold_left (fun acc (k,v) -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) "Elements:\n" map ;;

Hash table:

let map = Hashtbl.create 42;;
Hashtbl.add map 'A' 1;;
Hashtbl.add map 'B' 2;;
Hashtbl.add map 'C' 3;;

(* iterate over pairs *)
Hashtbl.iter (fun k v -> Printf.printf "key: %c - value: %d\n" k v) map ;;

(* in functional programming it is often more useful to fold over the elements *)
Hashtbl.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;

Functional binary search tree:

module CharMap = Map.Make (Char);;
let map = CharMap.empty;;
let map = CharMap.add 'A' 1 map;;
let map = CharMap.add 'B' 2 map;;
let map = CharMap.add 'C' 3 map;;

(* iterate over pairs *)
CharMap.iter (fun k v -> Printf.printf "key: %c - value: %d\n" k v) map ;;

(* in functional programming it is often more useful to fold over the elements *)
CharMap.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;

Ol

;;; create sample associative array
(define aa (list->ff '(
   (hello . 1)
   (world . 2)
   (! . 3))))

(print aa)
; ==> #((! . 3) (hello . 1) (world . 2))

;;; simplest iteration over all associative array (using ff-iter, lazy iterator)
(let loop ((kv (ff-iter aa)))
   (cond
      ((null? kv) #true)
      ((pair? kv)
         (print (car kv))
         (loop (cdr kv)))
      (else (loop (force kv)))))
; ==> (! . 3)
; ==> (hello . 1)
; ==> (world . 2)

;;; iteration with returning value (using ff-fold)
(print
   "folding result: "
   (ff-fold
      (lambda (result key value)
         (print "key: " key ", value: " value)
         (+ result 1))
      0
      aa))

; ==> key: !, value: 3
; ==> key: hello, value: 1
; ==> key: world, value: 2
; ==> folding result: 3

;;; same but right fold (using ff-foldr)
(print
   "rfolding result: "
   (ff-foldr
      (lambda (result key value)
         (print "key: " key ", value: " value)
         (+ result 1))
      0
      aa))

; ==> key: world, value: 2
; ==> key: hello, value: 1
; ==> key: !, value: 3
; ==> rfolding result: 3

;;; at least create new array from existing (let's multiply every value by value)
(define bb (ff-map aa
      (lambda (key value)
         (* value value))))
(print bb)

; ==> #((! . 9) (hello . 1) (world . 4))

ooRexx

d = .directory~new
d["hello"] = 1
d["world"] = 2
d["!"] = 3

-- iterating over keys:
loop key over d
    say "key =" key
end

-- iterating over values:
loop value over d~allitems
    say "value =" value
end

-- iterating over key-value pairs:
s = d~supplier
loop while s~available
    say "key =" s~index", value =" s~item
    s~next
end
Output:
key = !
key = world
key = hello
value = 3
value = 2
value = 1
key = !, value = 3
key = world, value = 2
key = hello, value = 1

Oz

declare
  MyMap = unit('hello':13 'world':31 '!':71)
in
  {ForAll {Record.toListInd MyMap} Show}  %% pairs
  {ForAll {Record.arity     MyMap} Show}  %% keys
  {ForAll {Record.toList    MyMap} Show}  %% values

PARI/GP

Works with: PARI/GP version 2.8.1+

The keys can be retried from a map with Vec:

keys = Vec(M);

You can iterate over the values as usual:

for(i=1,#keys,
  print(keys[i]," ",mapget(M,keys[i]))
)

Perl

#! /usr/bin/perl
use strict;

my %pairs = ( "hello" => 13,
	      "world" => 31,
	      "!" => 71 );

# iterate over pairs

# Be careful when using each(), however, because it uses a global iterator
# associated with the hash. If you call keys() or values() on the hash in the
# middle of the loop, the each() iterator will be reset to the beginning. If
# you call each() on the hash somewhere in the middle of the loop, it will
# skip over elements for the "outer" each(). Only use each() if you are sure
# that the code inside the loop will not call keys(), values(), or each().
while ( my ($k, $v) = each %pairs) {
    print "(k,v) = ($k, $v)\n";
}

# iterate over keys
foreach my $key ( keys %pairs ) {
    print "key = $key, value = $pairs{$key}\n";
}
# or (see note about each() above)
while ( my $key = each %pairs) {
    print "key = $key, value = $pairs{$key}\n";
}

# iterate over values
foreach my $val ( values %pairs ) {
    print "value = $val\n";
}

Phix

The first three lines create a simple dictionary, with keys and values of several different types (string/integer/sequence):

with javascript_semantics
setd("one",1)
setd(2,"duo")
setd({3,4},{5,"six"})
 
function visitor(object key, object data, object /*userdata*/)
    ?{key,data}
    return 1    -- (continue traversal)
end function
traverse_dict(routine_id("visitor"))
Output:
{2,"duo"}
{{3,4},{5,"six"}}
{"one",1}

You could also use some of the map.e routines:

with javascript_semantics
setd("one",1)
setd(2,"duo")
setd({3,4},{5,"six"})

requires("1.0.2") -- (map.e incompatible with p2js before that)
include builtins\map.e
?pairs()
?keys()
?values()
Output:
{{2,"duo"},{{3,4},{5,"six"}},{"one",1}}
{2,{3,4},"one"}
{"duo",{5,"six"},1}

Phixmonti

include ..\Utilitys.pmt

def getd    /# dict key -- dict data #/
    swap 1 get rot find nip
    dup if
        swap 2 get rot get nip
    else
        drop "Unfound"
    endif
enddef


def setd    /# dict ( key data ) -- dict #/
    1 get var ikey
    2 get var idata
    drop
    1 get ikey find var p drop
    p if
        2 get idata p set 2 set
    else
        2 get idata 0 put 2 set
        1 get ikey 0 put 1 set
    endif
enddef


def pair    /# dict n -- dict ( k d ) #/
    1 over 2 tolist var ikey
    2 swap 2 tolist var idata
    ikey sget
    swap idata sget rot swap
    2 tolist
enddef

def scandict    /# dict n -- dict ( ) #/
    var n
    1 get len nip
    for
        pair
        n if n get nip endif
        print nl
    endfor
enddef

def pairs   /# dict -- dict ( ) #/
    0 scandict  
enddef

def keys
    1 scandict  
enddef

def values
    2 scandict  
enddef

/# ---------- MAIN ---------- #/

( ( ) ( ) )

( "one" 1 ) setd
( 2 "duo" ) setd
( ( 3 4 ) ( 5 "six" ) ) setd

pairs nl
keys nl
values

PHP

<?php
$pairs = array( "hello" => 1,
		"world" => 2,
		"!"     => 3 );

// iterate over key-value pairs
foreach($pairs as $k => $v) {
  echo "(k,v) = ($k, $v)\n";
}

// iterate over keys
foreach(array_keys($pairs) as $key) {
  echo "key = $key, value = $pairs[$key]\n";
}

// iterate over values
foreach($pairs as $value) {
  echo "values = $value\n";
}
?>

Picat

go =>
  Map = new_map([1=one,2=two,3=three,4=four]),
  foreach(K=V in Map)
    println(K=V)
  end,
  nl,

  println(keys=Map.keys),  
  foreach(K in Map.keys.sort)
    println(K=Map.get(K))
  end,
  nl,

  println(values=Map.values),
  foreach(V in Map.values.sort)
    % This works but gets a warning: nonlocal_var_in_iterator_pattern
    % println(V=[K : K=V in Map])
    
    % No warning:
    println(V=[K : K=V1 in Map,V1 == V])
  end,
  nl.
Output:
1 = one
2 = two
3 = three
4 = four

keys = [1,2,3,4]
1 = one
2 = two
3 = three
4 = four

values = [one,two,three,four]
four = [4]
one = [1]
three = [3]
two = [2]


PicoLisp

Using properties

(put 'A 'foo 5)
(put 'A 'bar 10)
(put 'A 'baz 15)

: (getl 'A)                            # Get the whole property list
-> ((15 . baz) (10 . bar) (5 . foo))

: (mapcar cdr (getl 'A))               # Get all keys
-> (baz bar foo)

: (mapcar car (getl 'A))               # Get all values
-> (15 10 5)

Using an index tree

(idx 'A (def "foo" 5) T)
(idx 'A (def "bar" 10) T)
(idx 'A (def "baz" 15) T)

: A                                    # Get the whole tree
-> ("foo" ("bar" NIL "baz"))

:  (idx 'A)                            # Get all keys
-> ("bar" "baz" "foo")

:  (mapcar val (idx 'A))               # Get all values
-> (10 15 5)

Pike

note that the order is not alphabetic but depends on the hash value of the keys. the order is deterministic however.

mapping(string:string) m = ([ "A":"a", "B":"b", "C":"c" ]);
foreach(m; string key; string value)
{
    write(key+value);
}
Result: BbAaCc

// only keys
foreach(m; string key;)
{
    write(key);
}
Result: BAC

// only values
foreach(m;; string value)
{
    write(value);
}
Result: bac

PostScript

% over keys and values
<</a 1 /b 2 /c 3>> {= =} forall
% just keys
<</a 1 /b 2 /c 3>> {= } forall
% just values
<</a 1 /b 2 /c 3>> {pop =} forall

Potion

We can traverse tables by key or by key and val. We cannot traverse tables only by val.

mydictionary = (red=0xff0000, green=0x00ff00, blue=0x0000ff)

mydictionary each (key, val): (key, ":", val, "\n") join print.
mydictionary each (key): (key, "\n") join print.

PowerShell

Using the following hash table:

$h = @{ 'a' = 1; 'b' = 2; 'c' = 3 }

Iterating over the key/value pairs is slightly cumbersome as it requires an explicit call to GetEnumerator:

$h.GetEnumerator() | ForEach-Object { Write-Host Key: $_.Name, Value: $_.Value }

A foreach statement can also be used:

foreach ($e in $h.GetEnumerator()) {
    Write-Host Key: $e.Name, Value: $e.Value
}

Iterating over the keys:

$h.Keys | ForEach-Object { Write-Host Key: $_ }

foreach ($k in $h.Keys) {
    Write-Host Key: $k
}

Iterating over the values:

$h.Values | ForEach-Object { Write-Host Value: $_ }

foreach ($v in $h.Values) {
    Write-Host Value: $v
}

Prolog

Following the example at Associative Array Creation (with the understanding that using a predicate to store a hash does not prevent a "key" from having more than one value):

assert( mymap(key1,value1) ).
assert( mymap(key2,value1) ).

To perform the specific task at hand:

?- forall( mymap(Key,Value), writeln( [Key,Value]) ).

[key1,value1]
[key2,value1]

In Prolog, however, iteration is "built-in". For example:

?- mymap(key1, Y).
Y = value1.

?- mymap(X, value1).
X = key1 ;
X = key2.

To construct the list of keys:

?- findall( X, mymap(X,value1), Xs).
Xs = [key1, key2].

To construct the list of distinct values:

?- findall( Y, mymap(key1,Y), Ys).
Ys = [value1].

PureBasic

Hashes are a built-in type called Map in Purebasic.

NewMap dict.s()
dict("de") = "German"
dict("en") = "English"
dict("fr") = "French"

ForEach dict()
  Debug MapKey(dict()) + ":" + dict()
Next

Python

myDict = { "hello": 13,
	   "world": 31,
	   "!"    : 71 }

# iterating over key-value pairs:
for key, value in myDict.items():
    print ("key = %s, value = %s" % (key, value))

# iterating over keys:
for key in myDict:
    print ("key = %s" % key)
# (is a shortcut for:)
for key in myDict.keys():
    print ("key = %s" % key)

# iterating over values:
for value in myDict.values():
    print ("value = %s" % value)

QB64

'dictionary is not native data type of QB64
' here a dictionary engine using a string to store data
Dim Shared Skey As String * 1, SValue As String * 1, EValue As String * 1
Skey = Chr$(0)
SValue = Chr$(1)
EValue = Chr$(255)
 
'Demo area---------------->
Dim MyDictionary As String
 
If ChangeValue(MyDictionary, "a", "Ananas") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "b", "Banana") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "c", "cherry") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "d", "Drake") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "e", "Elm") Then Print "added new couple key value"
If ChangeValue(MyDictionary, "f", "Fire") Then Print "added new couple key value"
Print LenDict(MyDictionary)
Print "to key e there is  "; GetDict$(MyDictionary, "e")
Print "to key e there is  "; GetDict$(MyDictionary, "a")
If ChangeValue(MyDictionary, "e", "Elephant") Then Print " changed value of key passed"
Print "to key e there is  "; GetDict$(MyDictionary, "e")
If Not (EraseKeyValue(MyDictionary, "e")) Then Print " Failed to erase key value passed" Else Print "Erased key value passed"
If GetDict$(MyDictionary, "e") = "" Then Print " No couple key value found for key value 'e'"
If ChangeKey(MyDictionary, "e", "f") = 0 Then
    Print "key -a- has value "; GetDict$(MyDictionary, "a")
    Print "we change key a to key e "
    If ChangeKey(MyDictionary, "a", "e") = -1 Then
        Print "key -a- has value "; GetDict$(MyDictionary, "a")
        Print "key -e- has value "; GetDict$(MyDictionary, "e")
    End If
End If
If InsertCouple(MyDictionary, "c", "m", "mellon") = -1 Then
    Print " New couple inserted after key -c- "; GetDict$(MyDictionary, "c")
    Print " new couple is  key -m- "; GetDict$(MyDictionary, "m")
End If
Print LenDict(MyDictionary)
' End demo area --------------->
End
 
 
' it returns value/s for a key
Function GetDict$ (dict As String, Keys As String)
    Dim StartK As Integer, StartV As Integer, EndV As Integer
    StartK = InStr(dict, Skey + Keys + SValue)
    StartV = InStr(StartK, dict, SValue)
    EndV = InStr(StartV, dict, EValue)
    If StartK = 0 Then GetDict$ = "" Else GetDict = Mid$(dict, StartV + 1, EndV - StartV)
End Function
 
' it changes value of a key or append the couple key, newvalue if key is new
Function ChangeValue (dict As String, Keys As String, NewValue As String)
    ChangeValue = 0
    Dim StartK As Integer, StartV As Integer, EndV As Integer
    StartK = InStr(dict, Skey + Keys + SValue)
    StartV = InStr(StartK, dict, SValue)
    EndV = InStr(StartV, dict, EValue)
    If StartK = 0 Then
        dict = dict + Skey + Keys + SValue + NewValue + EValue
    Else
        dict = Left$(dict, StartV) + NewValue + Right$(dict, Len(dict) - EndV + 1)
    End If
    ChangeValue = -1
End Function
 
'it changes a key if it is in the dictionary
Function ChangeKey (dict As String, Keys As String, NewKey As String)
    ChangeKey = 0
    Dim StartK As Integer, StartV As Integer
    StartK = InStr(dict, Skey + Keys + SValue)
    StartV = InStr(StartK, dict, SValue)
    If StartK = 0 Then
        Print "Key " + Keys + " not found"
        Exit Function
    Else
        dict = Left$(dict, StartK) + NewKey + Right$(dict, Len(dict) - StartV + 1)
    End If
    ChangeKey = -1
End Function
 
'it erases the couple key value
Function EraseKeyValue (dict As String, keys As String)
    EraseKeyValue = 0
    Dim StartK As Integer, StartV As Integer, EndV As Integer
    StartK = InStr(dict, Skey + keys + SValue)
    StartV = InStr(StartK, dict, SValue)
    EndV = InStr(StartV, dict, EValue)
    If StartK = 0 Then
        Exit Function
    Else
        dict = Left$(dict, StartK - 1) + Right$(dict, Len(dict) - EndV + 1)
    End If
    EraseKeyValue = -1
End Function
 
'it inserts a couple after a defined key, if key is not in dictionary it append couple key value
Function InsertCouple (dict As String, SKeys As String, Keys As String, Value As String)
    InsertCouple = 0
    Dim StartK As Integer, StartV As Integer, EndV As Integer
    StartK = InStr(dict, Skey + SKeys + SValue)
    StartV = InStr(StartK, dict, SValue)
    EndV = InStr(StartV, dict, EValue)
    If StartK = 0 Then
        dict = dict + Skey + Keys + SValue + Value + EValue
    Else
        dict = Left$(dict, EndV) + Skey + Keys + SValue + Value + EValue + Right$(dict, Len(dict) - EndV + 1)
    End If
    InsertCouple = -1
End Function
 
Function LenDict (dict As String)
    LenDict = 0
    Dim a As Integer, count As Integer
    If Len(dict) <= 0 Then Exit Function
    While a <= Len(dict)
        a = InStr(a + 1, dict, EValue)
        If a > 0 Then count = count + 1 Else Exit While
    Wend
    LenDict = count
End Function

R

R lacks a native representation of key-value pairs, but different structures allow named elements, which provide similar functionality.

environment example

> env <- new.env()
> env[["x"]] <- 123
> env[["x"]]
[1] 123
> index <- "1"
> env[[index]] <- "rainfed hay"
> for (name in ls(env)) {
+   cat(sprintf('index=%s, value=%s\n', name, env[[name]]))
+ }
index=1, value=rainfed hay
index=x, value=123

vector example

> x <- c(hello=1, world=2, "!"=3)
> print(x["!"])
! 
3
> print(unname(x["!"]))
[1] 3

list example

> a <- list(a=1, b=2, c=3.14, d="xyz")
> print(a$a)
[1] 1
> print(a$d)
[1] "xyz"

Racket

Using the dictionary interface, different data structures can be treated as an associative array in Racket.

#lang racket

(define dict1 #hash((apple . 5) (orange . 10))) ; hash table
(define dict2 '((apple . 5) (orange . 10)))     ; a-list
(define dict3 (vector "a" "b" "c"))             ; vector (integer keys)

(dict-keys dict1)                   ; => '(orange apple)
(dict-values dict2)                 ; => '(5 10)
(for/list ([(k v) (in-dict dict3)]) ; => '("0 -> a" "1 -> b" "2 -> c")
  (format "~a -> ~a" k v))

Raku

(formerly Perl 6)

Works with: Rakudo version 2015.12
my %pairs = hello => 13, world => 31, '!' => 71;
 
for %pairs.kv -> $k, $v {
    say "(k,v) = ($k, $v)";
}

# Stable order
for %pairs.sort(*.value)>>.kv -> ($k, $v) {
    say "(k,v) = ($k, $v)";
}

{ say "$^a => $^b" } for %pairs.kv;

say "key = $_" for %pairs.keys;

say "value = $_" for %pairs.values;

REXX

/*REXX program demonstrates how to  set and display  values  for an  associative array. */
/*╔════════════════════════════════════════════════════════════════════════════════════╗
  ║ The (below) two REXX statements aren't really necessary,  but it shows how to      ║
  ║ define any and all entries in a associative array so that if a "key" is used that  ║
  ║ isn't defined, it can be displayed to indicate such,  or its value can be checked  ║
  ║ to determine if a particular associative array element has been set (defined).     ║
  ╚════════════════════════════════════════════════════════════════════════════════════╝*/
stateF.= ' [not defined yet] '                   /*sets any/all state  former  capitals.*/
stateN.= ' [not defined yet] '                   /*sets any/all state names.            */
w      = 0                                       /*the maximum  length  of a state name.*/
stateL =
/*╔════════════════════════════════════════════════════════════════════════════════════╗
  ║ The list of states (empty as of now).  It's convenient to have them in alphabetic  ║
  ║ order;  they'll be listed in the order as they are in the REXX program below).     ║
  ║ In REXX,  when a key is used  (for a stemmed array,  as they are called in REXX),  ║
  ║ and the key isn't assigned a value,  the key's  name  is stored (internally)  as   ║
  ║ uppercase  (Latin)  characters  (as in the examples below.   If the  key  has a    ║
  ║ a value, the key's value is used as is  (i.e.:  no upper translation is performed).║
  ║ Actually,  any characters can be used,  including blank(s)  and  non─displayable   ║
  ║ characters  (including   '00'x,   'ff'x,   commas,   periods,   quotes,   ···).    ║
  ╚════════════════════════════════════════════════════════════════════════════════════╝*/
call setSC 'al',  "Alabama"            ,  'Tuscaloosa'
call setSC 'ca',  "California"         ,  'Benicia'
call setSC 'co',  "Colorado"           ,  'Denver City'
call setSC 'ct',  "Connecticut"        ,  'Hartford and New Haven  (jointly)'
call setSC 'de',  "Delaware"           ,  'New-Castle'
call setSC 'ga',  "Georgia"            ,  'Milledgeville'
call setSC 'il',  "Illinois"           ,  'Vandalia'
call setSC 'in',  "Indiana"            ,  'Corydon'
call setSC 'ia',  "Iowa"               ,  'Iowa City'
call setSC 'la',  "Louisiana"          ,  'New Orleans'
call setSC 'me',  "Maine"              ,  'Portland'
call setSC 'mi',  "Michigan"           ,  'Detroit'
call setSC 'ms',  "Mississippi"        ,  'Natchez'
call setSC 'mo',  "Missouri"           ,  'Saint Charles'
call setSC 'mt',  "Montana"            ,  'Virginia City'
call setSC 'ne',  "Nebraska"           ,  'Lancaster'
call setSC 'nh',  "New Hampshire"      ,  'Exeter'
call setSC 'ny',  "New York"           ,  'New York'
call setSC 'nc',  "North Carolina"     ,  'Fayetteville'
call setSC 'oh',  "Ohio"               ,  'Chillicothe'
call setSC 'ok',  "Oklahoma"           ,  'Guthrie'
call setSC 'pa',  "Pennsylvania"       ,  'Lancaster'
call setSC 'sc',  "South Carolina"     ,  'Charlestown'
call setSC 'tn',  "Tennessee"          ,  'Murfreesboro'
call setSC 'vt',  "Vermont"            ,  'Windsor'

       do j=1  for words(stateL)                 /*show all capitals that were defined. */
       $= word(stateL, j)                        /*get the next (USA) state in the list.*/
       say 'the former capital of  ('$") "    left(stateN.$, w)      " was "      stateC.$
       end    /*j*/                              /* [↑]   show states that were defined.*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
setSC: parse arg code,name,cap;   upper code     /*get code, name & cap.; uppercase code*/
       stateL= stateL code                       /*keep a list of all the US state codes*/
       stateN.code= name; w= max(w,length(name)) /*define the state's name;  max width. */
       stateC.code= cap                          /*   "    "     "   code to the capital*/
       return                                    /*return to invoker, SETSC is finished.*/
output   when using the internal default input:
the former capital of  (AL)  Alabama         was  Tuscaloosa
the former capital of  (CA)  California      was  Benicia
the former capital of  (CO)  Colorado        was  Denver City
the former capital of  (CT)  Connecticut     was  Hartford and New Haven  (jointly)
the former capital of  (DE)  Delaware        was  New-Castle
the former capital of  (GA)  Georgia         was  Milledgeville
the former capital of  (IL)  Illinois        was  Vandalia
the former capital of  (IN)  Indiana         was  Corydon
the former capital of  (IA)  Iowa            was  Iowa City
the former capital of  (LA)  Louisiana       was  New Orleans
the former capital of  (ME)  Maine           was  Portland
the former capital of  (MI)  Michigan        was  Detroit
the former capital of  (MS)  Mississippi     was  Natchez
the former capital of  (MO)  Missouri        was  Saint Charles
the former capital of  (MT)  Montana         was  Virginia City
the former capital of  (NE)  Nebraska        was  Lancaster
the former capital of  (NH)  New Hampshire   was  Exeter
the former capital of  (NY)  New York        was  New York
the former capital of  (NC)  North Carolina  was  Fayetteville
the former capital of  (OH)  Ohio            was  Chillicothe
the former capital of  (OK)  Oklahoma        was  Guthrie
the former capital of  (PA)  Pennsylvania    was  Lancaster
the former capital of  (SC)  South Carolina  was  Charlestown
the former capital of  (TN)  Tennessee       was  Murfreesboro
the former capital of  (VT)  Vermont         was  Windsor

When this example was started, the intention was to list the former capitals by key.   Unfortunately, there's a duplicate capital   (Lancaster).

Ring

# Project : Associative array/Iteration

lst = [["hello", 13], ["world", 31], ["!", 71]]
for n = 1 to len(lst)
    see lst[n][1] + " : " + lst[n][2] + nl
next

Output:

hello : 13
world : 31
! : 71

RLaB

Associative arrays are called lists in RLaB.

x = <<>>;  // create an empty list
x.hello = 1;
x.world = 2;
x.["!"] = 3;

// to iterate over identifiers of a list one needs to use the function ''members''
// the identifiers are returned as a lexicographically ordered string row-vector
// here    ["!", "hello", "world"]
for(i in members(x)) 
{ printf("%s %g\n", i,  x.[i]); }

// occasionally one needs to check if there exists member of a list
y = members(x);  // y contains ["!", "hello", "world"]
clear(x.["!"]);  // remove member with identifier "!" from the list "x"
for(i in y) 
{ printf("%s %g\n", i,  x.[i]); }  // this produces error because x.["!"] does not exist

for(i in y) 
{ 
  if (exist(x.[i]))
  { printf("%s %g\n", i,  x.[i]); }  // we print a member of the list "x" only if it exists
}

Ruby

my_dict = { "hello" => 13,
	   "world" => 31,
	   "!"     => 71 }

# iterating over key-value pairs:
my_dict.each {|key, value| puts "key = #{key}, value = #{value}"}
# or
my_dict.each_pair {|key, value| puts "key = #{key}, value = #{value}"}

# iterating over keys:
my_dict.each_key {|key| puts "key = #{key}"}

# iterating over values:
my_dict.each_value {|value| puts "value =#{value}"}

another way:

for key, value in my_dict
  puts "key = #{key}, value = #{value}"
end

for key in my_dict.keys
  puts "key = #{key}"
end

for value in my_dict.values
  puts "value = #{value}"
end
Output:
key = hello, value = 13
key = world, value = 31
key = !, value = 71
key = hello
key = world
key = !
value = 13
value = 31
value = 71

Rust

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));
    for (country, medals) in olympic_medals {
        println!("{} has had {} gold medals, {} silver medals, and {} bronze medals", 
               country, medals.0, medals.1, medals.2);
            
    }
}
Output:

Note that HashMap does not preserve order (if this is important, std::collections::BTreeMap is what you want.)

Germany has had 252 gold medals, 260 silver medals, and 270 bronze medals
United States has had 1072 gold medals, 859 silver medals, and 749 bronze medals
Soviet Union has had 473 gold medals, 376 silver medals, and 355 bronze medals
Great Britain has had 246 gold medals, 276 silver medals, and 284 bronze medals

Scala

val m = Map("Amsterdam" -> "Netherlands", "New York" -> "USA", "Heemstede" -> "Netherlands")

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")
Output:

Key->Value: Amsterdam -> Netherlands, New York -> USA, Heemstede -> Netherlands Pairs: (Amsterdam,Netherlands), (New York,USA), (Heemstede,Netherlands) Keys: Amsterdam, New York, Heemstede Values: Netherlands, USA, Netherlands Unique values: Netherlands, USA

Scheme

Works with: Gauche Scheme
;; Create an associative array (hash-table) whose keys are strings:
(define table (hash-table 'string=?
  '("hello" . 0) '("world" . 22) '("!" . 999)))

;; Iterate over the table, passing the key and the value of each entry
;; as arguments to a function:
(hash-table-for-each
  table
  ;; Create by "partial application" a function that accepts 2 arguments,
  ;; the key and the value:
  (pa$ format #t "Key = ~a, Value = ~a\n"))

Output:

Key = !, Value = 999
Key = world, Value = 22
Key = hello, Value = 0
;; Iterate over the table and create a list of the keys and the
;; altered values:
(hash-table-map
  table
  (lambda (key val) (list key (+ val 5000))))

;; Create a new table that has the same keys but altered values.
(use gauche.collection)
(map-to <hash-table>
  (lambda (k-v) (cons (car k-v) (+ (cdr k-v) 5000)))
  table)

To get a list of the keys or of the values of the table, use one of the following:

(hash-table-keys table)
(hash-table-values table)

For persistent associative arrays

Works with: CHICKEN version 5.3.0
Library: r7rs
Library: srfi-1

Here is a variant of persistent associative arrays that includes generators. Such generators are functionally equivalent to iterators; the only important difference is they are executable procedures rather than passive objects. Note also that, because these associative arrays are persistent, iterating through their structure is much safer than it would be in a conventional hash table, which might change or even disappear while you were doing the iteration.

What I present here is a trimmed-down version of what you might find in a prefabricated library, such as an implementation of SRFI-125. I suppose part of my point in presenting this is that ‘associative arrays’ are not actually part of the Scheme language (even in R6RS), but rather are library add-ons. Someone else has done the sort of thing I am demonstrating here. This is in contrast to, say, Awk, or Icon, where associative ‘arrays’ really are built into the language.

(To save space, I have removed comments you can read at the section for persistent associative arrays.)

(cond-expand
  (r7rs)
  (chicken (import r7rs)))

(define-library (suspendable-procedures)

  (export &fail failure? success? suspend fail-forever
          make-generator-procedure)

  (import (scheme base))

  (begin

    (define-record-type <&fail>
      (make-the-one-unique-&fail-that-you-must-not-make-twice)
      do-not-use-this:&fail?)

    (define &fail
      (make-the-one-unique-&fail-that-you-must-not-make-twice))

    (define (failure? f) (eq? f &fail))
    (define (success? f) (not (failure? f)))
    (define *suspend* (make-parameter (lambda (x) x)))
    (define (suspend v) ((*suspend*) v))
    (define (fail-forever)
      (let loop ()
        (suspend &fail)
        (loop)))

    (define (make-generator-procedure thunk)
      ;; This is for making a suspendable procedure that takes no
      ;; arguments when resumed. The result is a simple generator of
      ;; values.
      (define (next-run return)
        (define (my-suspend v)
          (set! return (call/cc (lambda (resumption-point)
                                  (set! next-run resumption-point)
                                  (return v)))))
        (parameterize ((*suspend* my-suspend))
          (suspend (thunk))
          (fail-forever)))
      (lambda () (call/cc next-run)))

    )) ;; end library (suspendable-procedures)

(define-library (avl-trees)
  ;;
  ;; Persistent (that is, ‘immutable’) AVL trees for R7RS Scheme.
  ;;
  ;; References:
  ;;
  ;;   * Niklaus Wirth, 1976. Algorithms + Data Structures =
  ;;     Programs. Prentice-Hall, Englewood Cliffs, New Jersey.
  ;;
  ;;   * Niklaus Wirth, 2004. Algorithms and Data Structures. Updated
  ;;     by Fyodor Tkachov, 2014.
  ;;

  (export avl-make-generator)
  (export avl avl? avl-empty? avl-insert avl-search-values)
  (export avl-check-usage)

  (import (scheme base))
  (import (scheme case-lambda))
  (import (scheme process-context))
  (import (scheme write))
  (import (suspendable-procedures))

  (begin

    (define-syntax avl-check-usage
      (syntax-rules ()
        ((_ pred msg)
         (or pred (usage-error msg)))))

    (define-record-type <avl>
      (%avl key data bal left right)
      avl?
      (key %key)
      (data %data)
      (bal %bal)
      (left %left)
      (right %right))

    (define avl-make-generator
      (case-lambda
        ((tree) (avl-make-generator tree 1))
        ((tree direction)
         (if (negative? direction)
             (make-generator-procedure
              (lambda ()
                (define (traverse p)
                  (unless (or (not p) (avl-empty? p))
                    (traverse (%right p))
                    (suspend (cons (%key p) (%data p)))
                    (traverse (%left p)))
                  &fail)
                (traverse tree)))
             (make-generator-procedure
              (lambda ()
                (define (traverse p)
                  (unless (or (not p) (avl-empty? p))
                    (traverse (%left p))
                    (suspend (cons (%key p) (%data p)))
                    (traverse (%right p)))
                  &fail)
                (traverse tree)))))))

    (define (avl) (%avl #f #f #f #f #f))

    (define (avl-empty? tree)
      (avl-check-usage
       (avl? tree)
       "avl-empty? expects an AVL tree as argument")
      (not (%bal tree)))

    (define (avl-search-values pred<? tree key)
      (define (search p)
        (if (not p)
            (values #f #f)
            (let ((k (%key p)))
              (cond ((pred<? key k) (search (%left p)))
                    ((pred<? k key) (search (%right p)))
                    (else (values (%data p) #t))))))
      (avl-check-usage
       (procedure? pred<?)
       "avl-search-values expects a procedure as first argument")
      (if (avl-empty? tree)
          (values #f #f)
          (search tree)))

    (define (avl-insert pred<? tree key data)
      (define (search p fix-balance?)
        (cond
         ((not p)
          (values (%avl key data 0 #f #f) #t))
         ((pred<? key (%key p))
          (let-values (((p1 fix-balance?)
                        (search (%left p) fix-balance?)))
            (cond
             ((not fix-balance?)
              (let ((p^ (%avl (%key p) (%data p) (%bal p)
                              p1 (%right p))))
                (values p^ #f)))
             (else
              (case (%bal p)
                ((1)
                 (let ((p^ (%avl (%key p) (%data p) 0
                                 p1 (%right p))))
                   (values p^ #f)))
                ((0)
                 (let ((p^ (%avl (%key p) (%data p) -1
                                 p1 (%right p))))
                   (values p^ fix-balance?)))
                ((-1)
                 (case (%bal p1)
                   ((-1)
                    (let* ((p^ (%avl (%key p) (%data p) 0
                                     (%right p1) (%right p)))
                           (p1^ (%avl (%key p1) (%data p1) 0
                                      (%left p1) p^)))
                      (values p1^ #f)))
                   ((0 1)
                    (let* ((p2 (%right p1))
                           (bal2 (%bal p2))
                           (p^ (%avl (%key p) (%data p)
                                     (- (min bal2 0))
                                     (%right p2) (%right p)))
                           (p1^ (%avl (%key p1) (%data p1)
                                      (- (max bal2 0))
                                      (%left p1) (%left p2)))
                           (p2^ (%avl (%key p2) (%data p2) 0
                                      p1^ p^)))
                      (values p2^ #f)))
                   (else (internal-error))))
                (else (internal-error)))))))

         ((pred<? (%key p) key)
          (let-values (((p1 fix-balance?)
                        (search (%right p) fix-balance?)))
            (cond
             ((not fix-balance?)
              (let ((p^ (%avl (%key p) (%data p) (%bal p)
                              (%left p) p1)))
                (values p^ #f)))
             (else
              (case (%bal p)
                ((-1)
                 (let ((p^ (%avl (%key p) (%data p) 0
                                 (%left p) p1)))
                   (values p^ #f)))
                ((0)
                 (let ((p^ (%avl (%key p) (%data p) 1
                                 (%left p) p1)))
                   (values p^ fix-balance?)))
                ((1)
                 (case (%bal p1)
                   ((1)
                    (let* ((p^ (%avl (%key p) (%data p) 0
                                     (%left p) (%left p1)))
                           (p1^ (%avl (%key p1) (%data p1) 0
                                      p^ (%right p1))))
                      (values p1^ #f)))
                   ((-1 0)
                    (let* ((p2 (%left p1))
                           (bal2 (%bal p2))
                           (p^ (%avl (%key p) (%data p)
                                     (- (max bal2 0))
                                     (%left p) (%left p2)))
                           (p1^ (%avl (%key p1) (%data p1)
                                      (- (min bal2 0))
                                      (%right p2) (%right p1)))
                           (p2^ (%avl (%key p2) (%data p2) 0
                                      p^ p1^)))
                      (values p2^ #f)))
                   (else (internal-error))))
                (else (internal-error)))))))
         (else
          (values (%avl key data (%bal p) (%left p) (%right p))
                  #f))))
      (avl-check-usage
       (procedure? pred<?)
       "avl-insert expects a procedure as first argument")
      (if (avl-empty? tree)
          (%avl key data 0 #f #f)
          (let-values (((p fix-balance?) (search tree #f)))
            p)))

    (define (internal-error)
      (display "internal error\n" (current-error-port))
      (emergency-exit 123))

    (define (usage-error msg)
      (display "Procedure usage error:\n" (current-error-port))
      (display "  " (current-error-port))
      (display msg (current-error-port))
      (newline (current-error-port))
      (exit 1))

    )) ;; end library (avl-trees)


(define-library (associative-arrays)
  ;;
  ;; Persistent associative ‘arrays’ for R7RS Scheme.
  ;;
  ;; The structure is not actually an array, but is made of AVL trees
  ;; and association lists. Given a good hash function, it should
  ;; average logarithmic performance.
  ;;

  (export assoc-array-make-pair-generator
          assoc-array-make-key-generator
          assoc-array-make-data-generator)
  (export assoc-array assoc-array? assoc-array-set assoc-array-ref)

  (import (scheme base))
  (import (scheme case-lambda))
  (import (scheme write))
  (import (suspendable-procedures))
  (import (avl-trees))

  (cond-expand
    (chicken (import (only (srfi 1) alist-delete)))
    ;; Insert whatever you need here for your Scheme.
    (else))

  (begin

    (define-record-type <assoc-array>
      (%assoc-array hashfunc pred=? default table)
      assoc-array?
      (hashfunc %hashfunc)
      (pred=? %pred=?)
      (default %default)
      (table %table))

    (define (assoc-array-make-generator array kind)
      (define tree-traverser (avl-make-generator (%table array)))
      (define get-desired-part
        (cond ((eq? kind 'key) (lambda (pair) (car pair)))
              ((eq? kind 'data) (lambda (pair) (cdr pair)))
              (else (lambda (pair) pair))))
      (make-generator-procedure
       (lambda ()
         (let traverse ()
           (let ((tree-entry (tree-traverser)))
             (when (success? tree-entry)
               (let scan-lst ((lst (cdr tree-entry)))
                 (when (pair? lst)
                   (suspend (get-desired-part (car lst)))
                   (scan-lst (cdr lst))))
               (traverse))))
         &fail)))

    (define (assoc-array-make-pair-generator array)
      (assoc-array-make-generator array 'pair))

    (define (assoc-array-make-key-generator array)
      (assoc-array-make-generator array 'key))

    (define (assoc-array-make-data-generator array)
      (assoc-array-make-generator array 'data))

    (define assoc-array
      (case-lambda
        ((hashfunc)
         (let ((pred=? equal?)
               (default #f))
           (assoc-array hashfunc pred=? default)))
        ((hashfunc pred=?)
         (let ((default #f))
           (assoc-array hashfunc pred=? default)))
        ((hashfunc pred=? default)
         (%assoc-array hashfunc pred=? default (avl)))))

    (define (assoc-array-set array key data)
      (let ((hashfunc (%hashfunc array))
            (pred=? (%pred=? array))
            (default (%default array))
            (table (%table array)))
        (let ((hash-value (hashfunc key)))
          (let*-values
              (((alst found?) (avl-search-values < table hash-value)))
            (cond
             (found?
              (let* ((alst (alist-delete key alst pred=?))
                     (alst `((,key . ,data) . ,alst))
                     (table (avl-insert < table hash-value alst)))
                (%assoc-array hashfunc pred=? default table)))
             (else
              (let* ((alst `((,key . ,data)))
                     (table (avl-insert < table hash-value alst)))
                (%assoc-array hashfunc pred=? default table))))))))

    (define (assoc-array-ref array key)
      (let* ((hashfunc (%hashfunc array))
             (hash-value (hashfunc key)))
        (let*-values
            (((alst found?)
              (avl-search-values < (%table array) hash-value)))
          (if found?
              (let ((pair (assoc key alst (%pred=? array))))
                (if pair
                    (cdr pair)
                    (%default array)))
              (%default array)))))

    )) ;; end library (associative-arrays)


(cond-expand
  (DEMONSTRATION
   (begin
     (import (scheme base))
     (import (scheme write))
     (import (suspendable-procedures))
     (import (associative-arrays))

     (define (hashfunc s)
       ;; Using Knuth’s random number generator to concoct a quick and
       ;; dirty and probably very bad hash function. It should be much
       ;; better to use something like SpookyHash, but this is a demo.
       (define a 6364136223846793005)
 	   (define c 1442695040888963407)
       (define M (expt 2 64))
       (let ((n (string-length s))
             (h 123))
         (do ((i 0 (+ i 1)))
             ((= i n))
           (let* ((x (char->integer (string-ref s i)))
                  (x (+ (* a (+ h x)) c)))
             (set! h (truncate-remainder x M))))
         h))

     (define a (assoc-array hashfunc))

     ;; Fill the associative array ‘a’ with (string . number)
     ;; associations.
     (do ((i 1 (+ i 1)))
         ((= i 11))
       (set! a (assoc-array-set a (number->string i) i)))

     ;; Go through the association pairs (in arbitrary order) with a
     ;; generator.
     (let ((gen (assoc-array-make-pair-generator a)))
       (do ((pair (gen) (gen)))
           ((failure? pair))
         (write pair) (display " "))
       (newline))

     ;; Go through the keys (in arbitrary order) with a generator.
     (let ((gen (assoc-array-make-key-generator a)))
       (do ((key (gen) (gen)))
           ((failure? key))
         (write key) (display " "))
       (newline))

     ;; Go through the values (in arbitrary order) with a generator.
     (let ((gen (assoc-array-make-data-generator a)))
       (do ((value (gen) (gen)))
           ((failure? value))
         (write value) (display " "))
       (newline))

     ))
  (else))
Output:
$ csc -DDEMONSTRATION -R r7rs -X r7rs associative_array_with_generators.scm && ./associative_array_with_generators
("3" . 3) ("6" . 6) ("9" . 9) ("1" . 1) ("4" . 4) ("7" . 7) ("2" . 2) ("10" . 10) ("5" . 5) ("8" . 8) 
"3" "6" "9" "1" "4" "7" "2" "10" "5" "8" 
3 6 9 1 4 7 2 10 5 8

Seed7

$ include "seed7_05.s7i";

const type: dictType is hash [string] integer;
var dictType: myDict is dictType.value;

const proc: main is func
  local
    var string: stri is "";
    var integer: number is 0;
  begin
    myDict @:= ["hello"] 1;
    myDict @:= ["world"] 2;
    myDict @:= ["!"] 3;

    # iterating over key-value pairs:
    for number key stri range myDict do
      writeln("key = " <& number <& ", value = " <& stri);
    end for;

    # iterating over keys:
    for key stri range myDict do
      writeln("key = " <& stri);
    end for;

    # iterating over values:
    for number range myDict do
      writeln("value = " <& number);
    end for;
  end func;
Output:
key = 3, value = !
key = 1, value = hello
key = 2, value = world
key = !
key = hello
key = world
value = 3
value = 1
value = 2

SenseTalk

put {name:"Fluffy", type:"Rabbit", color:"White"} into animal
put "Carries a watch" into animal's habits

put "The animal: " & animal
put "The keys: " & keys of animal
put "The values: " & animal's values
// Keys and Values
put ,"All Properties:"
repeat with each [key,value] in animal
	put !"Key: [[key]]  Value: [[value]]"
end repeat

// Keys only
put ,"Keys:"
repeat with each [key] in animal
	put key
end repeat

// Values only
put ,"Values:"
repeat with each [,value] in animal
	put value
end repeat

// Using an iterator
put ,"Treating the property list as an iterator:"
put animal's nextValue -- calling any of the "next" functions begins iteration
put animal's nextKeyValue
put animal's nextKey
put animal's nextKeyValue
put animal's nextValue -- walking off the end returns a unique endValue
Output:
The animal: {color:"White", habits:"Carries a watch", name:"Fluffy", type:"Rabbit"}
The keys: ["color","habits","name","type"]
The values: ["White","Carries a watch","Fluffy","Rabbit"]

All Properties:
Key: color  Value: White
Key: habits  Value: Carries a watch
Key: name  Value: Fluffy
Key: type  Value: Rabbit

Keys:
color
habits
name
type

Values:
White
Carries a watch
Fluffy
Rabbit

Treating the property list as an iterator:
White
["habits","Carries a watch"]
name
["type","Rabbit"]
    ⓔ ⓝ ⓓ    

Sidef

var hash = Hash(
    key1 => 'value1',
    key2 => 'value2',
)

# Iterate over key-value pairs
hash.each { |key, value|
    say "#{key}: #{value}"
}

# Iterate only over keys
hash.keys.each { |key|
    say key
}

# Iterate only over values
hash.values.each { |value|
    say value
}
Output:
key1: value1
key2: value2
key1
key2
value1
value2

Slate

In Slate, all associative mappings inherit from Mapping, so they all have the same protocol. Even Sequences obey it, in addition to their own protocol for collections with ordered integer-range keys.

define: #pairs -> ({'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3} as: Dictionary).
pairs keysAndValuesDo: [| :key :value |
  inform: '(k, v) = (' ; key printString ; ', ' ; value printString ; ')'
].

pairs keysDo: [| :key |
  inform: '(k, v) = (' ; key printString ; ', ' ; (pairs at: key) printString ; ')'
].

pairs do: [| :value |
  inform: 'value = ' ; value printString
].

Smalltalk

Works with: GNU Smalltalk
|pairs|
pairs := Dictionary 
	    from: { 'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3 }.

"iterate over keys and values"
pairs keysAndValuesDo: [ :k :v |
    ('(k, v) = (%1, %2)' % { k. v }) displayNl
].

"iterate over keys"
pairs keysDo: [ :key |
    ('key = %1, value = %2' % { key. pairs at: key }) displayNl
].

"iterate over values"
pairs do: [ :value |
    ('value = %1' % { value }) displayNl
].

We could also obtain a set of keys or a collection of values and iterate over them with "do:":

(pairs keys) do: [ :k | "..." ].
(pairs values) do: [ :v | "..." ].

SNOBOL4

Works with: Macro Spitbol
Works with: Snobol4+
Works with: CSnobol
*       # Create sample table
        t = table()
        t<'cat'> = 'meow'
        t<'dog'> = 'woof'
        t<'pig'> = 'oink'

*       # Convert table to key/value array
        a = convert(t,'array')

*       # Iterate pairs
ploop   i = i + 1; output = a<i,1> ' -> ' a<i,2> :s(ploop)
*       # Iterate keys
kloop   j = j + 1; output = a<j,1> :s(kloop)
*       # Iterate vals
vloop   k = k + 1; output = a<k,2> :s(vloop)
end

Stata

mata
// Create an associative array
a=asarray_create()
asarray(a,"one",1)
asarray(a,"two",2)

// Loop over entries
loc=asarray_first(a)
do {
	printf("%s %f\n",asarray_key(a,loc),asarray_contents(a,loc))
	loc=asarray_next(a,loc)
} while(loc!=NULL)
end

Swift

let myMap = [
	   "hello": 13,
	   "world": 31,
	   "!"    : 71 ]

// iterating over key-value pairs:
for (key, value) in myMap {
	print("key = \(key), value = \(value)")
}
// Just the keys
for key in myMap.keys
{
	print("key = \(key)")
}
// Just the values
for value in myMap.values
{
	print("value = \(value)")
}

Tcl

With Arrays

array set myAry {
    # list items here...
}

# Iterate over keys and values
foreach {key value} [array get myAry] {
    puts "$key -> $value"
}

# Iterate over just keys
foreach key [array names myAry] {
    puts "key = $key"
}

# There is nothing for directly iterating over just the values
# Use the keys+values version and ignore the keys

With Dictionaries

Works with: Tcl version 8.5
set myDict [dict create ...]; # Make the dictionary

# Iterate over keys and values
dict for {key value} $myDict {
    puts "$key -> $value"
}

# Iterate over keys
foreach key [dict keys $myDict] {
    puts "key = $key"
}

# Iterate over values
foreach value [dict values $myDict] {
    puts "value = $value"
}

TXR

(defvarl h (hash))

(each ((k '(a b c))
       (v '(1 2 3)))
  (set [h k] v))

(dohash (k v h)
  (put-line `@k -> @v`))
Run:
$ txr hash.tl 
c -> 3
b -> 2
a -> 1

UNIX Shell

At least two shells have associative arrays, but they use different syntax to access their keys.

Works with: ksh93
Works with: bash version 4.0 and above
typeset -A a=([key1]=value1 [key2]=value2)

# just keys
printf '%s\n' "${!a[@]}"

# just values
printf '%s\n' "${a[@]}"

# keys and values
for key in "${!a[@]}"; do
	printf '%s => %s\n' "$key" "${a[$key]}"
done
Works with: zsh
typeset -A a
a=(key1 value1 key2 value2)

# just keys
print -l -- ${(k)a}

# just values
print -l -- ${(v)a}

# keys and values
printf '%s => %s\n' ${(kv)a}

Vala

Library: Gee
using Gee;

void main(){
    // declare HashMap                                                          
    var map = new HashMap<string, double?>();

    // set 3 entries                                                            
    map["pi"] = 3.14;
    map["e"] = 2.72;
    map["golden"] = 1.62;

    // iterate over (key,value) pair                                            
    foreach (var elem in map.entries){
        string name = elem.key;
        double num = elem.value;

	stdout.printf("%s,%f\n", name, num);
    }

    // iterate over keys                                                        
    foreach (string key in map.keys){
	stdout.printf("%s\n", key);
    }

    // iterate over values                                                      
    foreach (double num in map.values){
	stdout.printf("%f\n", num);
    }
}

Compile with flag:

--pkg gee-1.0
Output:
e,2.720000
golden,1.620000
pi,3.140000
e
golden
pi
2.720000
1.620000
3.140000

VBA

Dictionaries are similar in VBA and VBScript. Here is how to iterate.

Option Explicit
Sub Test()
    Dim h As Object, i As Long, u, v, s
    Set h = CreateObject("Scripting.Dictionary")
    h.Add "A", 1
    h.Add "B", 2
    h.Add "C", 3

    'Iterate on keys
    For Each s In h.Keys
        Debug.Print s
    Next

    'Iterate on values
    For Each s In h.Items
        Debug.Print s
    Next

    'Iterate on both keys and values by creating two arrays
    u = h.Keys
    v = h.Items
    For i = 0 To h.Count - 1
        Debug.Print u(i), v(i)
    Next
End Sub

VBScript

'instantiate the dictionary object
Set dict = CreateObject("Scripting.Dictionary")

'populate the dictionary or hash table
dict.Add 1,"larry"
dict.Add 2,"curly"
dict.Add 3,"moe"

'iterate key and value pairs
For Each key In dict.Keys
	WScript.StdOut.WriteLine key & " - " & dict.Item(key)
Next
Output:
1 - larry
2 - curly
3 - moe

Vim Script

let dict = {"apples": 11, "oranges": 25, "pears": 4}

echo "Iterating over key-value pairs"
for [key, value] in items(dict)
    echo key " => " value
endfor
echo "\n"

echo "Iterating over keys"
for key in keys(dict)
    echo key
endfor
echo "\n"

echo "Iterating over values"
for value in values(dict)
    echo value
endfor
Output:
Iterating over key-value pairs                                                  
oranges  =>  25                                                                 
pears  =>  4                                                                    
apples  =>  11                                                                  
                                                                                
Iterating over keys                                                             
oranges                                                                         
pears                                                                           
apples                                                                          
                                                                                
Iterating over values                                                           
25                                                                              
4                                                                               
11

V (Vlang)

fn main() {
    my_map := {
        "hello": 13,
        "world": 31,
        "!"    : 71 }
    
    // iterating over key-value pairs:
    for key, value in my_map {
        println("key = $key, value = $value")
    }
    
    // iterating over keys:
    for key,_ in my_map {
        println("key = $key")
    }
    
    // iterating over values:
    for _, value in my_map {
        println("value = $value")
    }
}
Output:
key = hello, value = 13
key = world, value = 31
key = !, value = 71
key = hello
key = world
key = !
value = 13
value = 31
value = 71

Wart

h <- (table 'a 1 'b 2)
each (key val) table
  prn key " " val
Output:
a 1
b 2

Wren

Note that Wren makes no guarantee about iteration order which is not necessarily the same order in which the entries were added.

// create a new map with four entries
var capitals = {
    "France": "Paris",
    "Germany": "Berlin",
    "Russia": "Moscow",
    "Spain": "Madrid"
}

// iterate through the map and print out the key/value pairs
for (c in capitals) System.print([c.key, c.value])
System.print()

// iterate though the map and print out just the keys
for (k in capitals.keys) System.print(k)
System.print()

// iterate though the map and print out just the values
for (v in capitals.values) System.print(v)
Output:
[France, Paris]
[Russia, Moscow]
[Germany, Berlin]
[Spain, Madrid]

France
Russia
Germany
Spain

Paris
Moscow
Berlin
Madrid

XPL0

include c:\cxpl\stdlib;
char Dict(10,10);
int  Entries;

proc AddEntry(Letter, Greek);   \Insert entry into associative array
char Letter, Greek;
[Dict(Entries,0):= Letter;
StrCopy(Greek, @Dict(Entries,1));
Entries:= Entries+1;            \(limit checks ignored for simplicity)
];

int I;
[Entries:= 0;
AddEntry(^A, "alpha");
AddEntry(^D, "delta");
AddEntry(^B, "beta");
AddEntry(^C, "gamma");
for I:= 0 to Entries-1 do
    [ChOut(0, Dict(I,0));  ChOut(0, ^ );  Text(0, @Dict(I,1));  CrLf(0)];
]
Output:
A alpha
D delta
B beta
C gamma

zkl

var d=Dictionary("A","alpha","D","delta", "B","beta", "C", "gamma");
d.keys.pump(Console.print,fcn(k){String(k,",")})
d.values.apply("toUpper").println();
d.makeReadOnly();  // can only iterate over k,v pairs if read only
foreach k,v in (d){print(k,":",v,"; ")}
Output:
A,B,C,D,
L("ALPHA","BETA","GAMMA","DELTA")
A:alpha; B:beta; C:gamma; D:delta;