Associative array/Creation: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) |
|||
(6 intermediate revisions by 5 users not shown) | |||
Line 2,749:
# typed (coerces to float)</syntaxhighlight>
=={{header|EasyLang}}==
# use array of array for this
<syntaxhighlight lang="easylang">▼
▲</syntaxhighlight>
<syntaxhighlight lang="easylang">▼
▲proc indexAssoc index . array[][] item .
▲ for i = 1 to len array[][]
▲ if array[i][1] = index
▲ item = array[i][2]
▲ break 2
.
.
item$ =
.
proc
for i
if
.
.
.
clothing$[][] = [ [ "type" "t-shirt" ] [ "color" "red" ] ]
clothing$[][] &= [ "size" "xl" ]
#
hashSet "color" "green" clothing$[][]
hashGet "color" clothing$[][] col$
print col$
</syntaxhighlight>
Line 2,809 ⟶ 2,810:
<syntaxhighlight lang="java">Int? mightBeNull = map["foo"];
Int neverNull = map.getOrDefault("foo", 0);
if (Int n := map.get("foo")) {
// if "foo" is in the map, then the variable "n" is set to its value
} else {▼
▲else
// if "foo" is not in the map, then the variable "n" is not defined
Iterate over keys:
<syntaxhighlight lang="java">for (String key : map) {
// the variable "key" is defined here
Iterate over values:
<syntaxhighlight lang="java">for (Int value : map.values) {
// the variable "value" is defined here
Iterate over key, value pairs:
<syntaxhighlight lang="java">for ((String key, Int value) : map) {
// the variables "key" and "value" are defined here
=={{header|Elena}}==
Line 3,445 ⟶ 3,440:
say "'baz' => [V].";
end the story.</syntaxhighlight>
=={{header|Insitux}}==
It is possible to use any value type for both keys and values for a dictionary in Insitux.
:a "value" ;keyword key, string value
:b 123 ;keyword key, number value
456 [1 2 3] ;number key, vector value
[5 6 7] :b ;vector key, keyword value
{:a 1} {:b 2} ;dictionary key, dictionary value
<syntaxhighlight lang="insitux">;use dictionary as function for lookup; commas for readability, treated as white-space
> ({:a 1, :b 2, :c 3} :b)
2</syntaxhighlight>
<syntaxhighlight lang="insitux">;extend existing dictionary by using it as a function with two arguments
> ({:a 1, :b 2, :c 3} :b 3)
{:a 1, :b 3, :c 3}</syntaxhighlight>
=={{header|Ioke}}==
Line 3,918 ⟶ 3,933:
Hash keys in langur may be numbers or strings. Number keys are simplified, so that 1.0 is the same key as 1.
<syntaxhighlight lang="langur">var .hash =
# may assign with existing or non-existing hash key (if hash is mutable)
Line 6,020 ⟶ 6,035:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var hash = Hash
key1 => 'value1',
key2 => 'value2',
)
# Add a new key-value pair
hash{:key3} = 'value3'
=={{header|Slate}}==
Line 6,404 ⟶ 6,419:
=={{header|Wren}}==
Wren has a Map class built in.
<syntaxhighlight lang="
fruit[1] = "orange" // associates a key of 1 with "orange"
fruit[2] = "apple" // associates a key of 2 with "apple"
Line 6,433 ⟶ 6,448:
null
</pre>
=={{header|XLISP}}==
|