Associative array/Iteration: Difference between revisions

(Jakt)
(8 intermediate revisions by 7 users not shown)
Line 925:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
# use array of array for this
associative$[][] = [ [ 1 "associative" ] [ 2 "arrays" ] ]
clothing$[][] = [ [ "type" "t-shirt" ] [ "color" "red" ] [ "size" "xl" ] ]
print "Key-value pairs:"
for i = 1 to len associativeclothing$[][]
print associativeclothing$[i][1] & ": " & associativeclothing$[i][2]
.</syntaxhighlight>
.
print "Just keys:"
for i = 1 to len associative$[][]
print associative$[i][1]
.
print "Just values:"
for i = 1 to len associative$[][]
print associative$[i][2]
.
</syntaxhighlight>
{{out}}
<pre>
Key-value pairs:
1 associative
2 arrays
Just keys:
1
2
Just values:
associative
arrays
</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,812 ⟶ 1,830:
 
=={{header|Java}}==
<p>
<syntaxhighlight lang="java">Map<String, Integer> map = new HashMap<String, Integer>();
See also, [https://rosettacode.org/wiki/Associative_array/Creation#Java Java - Associative array/Creation].
map.put("hello", 1);
</p>
map.put("world", 2);
<p>
map.put("!", 3);
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 />
// iterating over key-value pairs:
It's worth noting that a <code>Map.Entry</code> also has the <code>setValue</code> method.
for (Map.Entry<String, Integer> e : map.entrySet()) {
</p>
String key = e.getKey();
<syntaxhighlight lang="java">
Integer value = e.getValue();
for (Map.Entry<String, Integer> entry : map.entrySet())
System.out.println("key = " + key + ", value = " + value);
System.out.println(entry);
}
</syntaxhighlight>
 
<p>
// iterating over keys:
You can access just the <kbd>key</kbd>s by using the <code>Map.keySet</code> method, which will return a <code>Set</code>.
for (String key : map.keySet()) {
</p>
System.out.println("key = " + key);
<syntaxhighlight lang="java">
}
for (String key : map.keySet())
 
System.out.println(key);
// iterating over values:
</syntaxhighlight>
for (Integer value : map.values()) {
<p>
System.out.println("value = " + value);
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>.
}</syntaxhighlight>
</p>
<syntaxhighlight lang="java">
for (int value : map.values())
System.out.println(value);
</syntaxhighlight>
<br />
 
Java 8 version
Line 1,993 ⟶ 2,017:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">fun main(a: Array<String>) {
val map = mapOf("hello" to 1, "world" to 2, "!" to 3)
 
with(map) {
entries.forEach { println("key = ${it.key}, value = ${it.value}") }
keys.forEach { println("key = $it") }
values.forEach { println("value = $it") }
Line 4,115 ⟶ 4,139:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var hash = Hash.new(
key1 => 'value1',
key2 => 'value2',
Line 4,122 ⟶ 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,300 ⟶ 4,324:
 
=={{header|UNIX Shell}}==
TwoAt least two shells have associative arrays, but they use different syntax to access their keys.
 
{{works with|ksh93}}
{{works with|bash|4.0 and above}}
<syntaxhighlight lang="bash">typeset -A a=([key1]=value1 [key2]=value2)
 
Line 4,515 ⟶ 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="ecmascriptwren">// create a new map with four entries
var capitals = {
"France": "Paris",
1,934

edits