Associative array/Iteration: Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
 
(16 intermediate revisions by 13 users not shown)
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").
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang="qbasic">DECLARE associative ASSOC STRING
 
Line 427 ⟶ 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.
 
==={{header|BASIC256}}===
''Solution is at [[Associative_array/Creation#BASIC256]]''.
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> REM Store some values with their keys:
PROCputdict(mydict$, "FF0000", "red")
Line 922 ⟶ 923:
println(`$key .`)
}</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}}==
Line 1,014 ⟶ 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,420 ⟶ 1,468:
1
</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}}==
Line 1,659 ⟶ 1,805:
Note that this last is not likely to be useful in any practical context outside of learning the language.
 
=={{header|JavaJakt}}==
<syntaxhighlight lang="javajakt">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);
</p>
}</syntaxhighlight>
<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,841 ⟶ 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 3,963 ⟶ 4,139:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var hash = Hash.new(
key1 => 'value1',
key2 => 'value2',
Line 3,970 ⟶ 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,079 ⟶ 4,255:
// iterating over key-value pairs:
for (key, value) in myMap {
println 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)")
}</syntaxhighlight>
 
Line 4,138 ⟶ 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,307 ⟶ 4,494:
11</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
my_map := {
"hello": 13,
Line 4,353 ⟶ 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