Associative array/Iteration: Difference between revisions
→{{header|UNIX Shell}}: mention bash
(→{{header|UNIX Shell}}: mention bash) |
|||
(7 intermediate revisions by 7 users not shown) | |||
Line 925:
=={{header|EasyLang}}==
<syntaxhighlight
# use array of array for this
clothing$[][] = [ [ "type" "t-shirt" ] [ "color" "red" ] [ "size" "xl" ] ]
for i
print
.</syntaxhighlight>▼
▲</syntaxhighlight>
{{out}}
<pre>
Just keys:▼
Just values:▼
</pre>▼
=={{header|EchoLisp}}==
Line 1,044 ⟶ 1,025:
3
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]
Italy
France
Germany
Spain
Rome
Paris
Berlin
Madrid
</pre>
Line 1,813 ⟶ 1,831:
=={{header|Java}}==
<p>
See also, [https://rosettacode.org/wiki/Associative_array/
</p>
<p>
Line 1,999 ⟶ 2,017:
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">fun main(
val map = mapOf("hello" to 1, "world" to 2, "!" to 3)
with(map) {
keys.forEach { println("key = $it") }
values.forEach { println("value = $it") }
Line 4,121 ⟶ 4,139:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var hash = Hash
key1 => 'value1',
key2 => 'value2',
Line 4,128 ⟶ 4,146:
# 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
}</syntaxhighlight>
{{out}}
Line 4,306 ⟶ 4,324:
=={{header|UNIX Shell}}==
{{works with|ksh93}}
{{works with|bash|4.0 and above}}
<syntaxhighlight lang="bash">typeset -A a=([key1]=value1 [key2]=value2)
Line 4,521 ⟶ 4,540:
=={{header|Wren}}==
Note that Wren makes no guarantee about iteration order which is not necessarily the same order in which the entries were added.
<syntaxhighlight lang="
var capitals = {
"France": "Paris",
|