Associative array/Iteration: Difference between revisions
→{{header|UNIX Shell}}: mention bash
m (BaCon, BASIC256, and BBC BASIC moved to the BASIC section.) |
(→{{header|UNIX Shell}}: mention bash) |
||
(9 intermediate revisions by 8 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>
{{out}}
<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]
== keys ==
Italy
France
Germany
Spain
== values ==
Rome
Paris
Berlin
Madrid
</pre>
Line 1,787 ⟶ 1,805:
Note that this last is not likely to be useful in any practical context outside of learning the language.
=={{header|
<syntaxhighlight lang="
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() {
}
}
</syntaxhighlight>
{{out}}
<pre>
("bar", 2)
("foo", 1)
bar
foo
</pre>
=={{header|Java}}==
<p>
See also, [https://rosettacode.org/wiki/Associative_array/Creation#Java Java - Associative array/Creation].
</p>
<p>
You can access the <kbd>key</kbd> and <kbd>value</kbd> pairs by using the <code>Map.entrySet</code> method,
which will return a <code>Map.Entry</code>.<br />
It's worth noting that a <code>Map.Entry</code> also has the <code>setValue</code> method.
</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
Line 1,969 ⟶ 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,091 ⟶ 4,139:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var hash = Hash
key1 => 'value1',
key2 => 'value2',
Line 4,098 ⟶ 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,276 ⟶ 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,491 ⟶ 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",
|