Associative array/Creation: Difference between revisions

 
(7 intermediate revisions by 5 users not shown)
Line 2,749:
# typed (coerces to float)</syntaxhighlight>
=={{header|EasyLang}}==
</syntaxhighlight>
EasyLang does not really have associative arrays, but arrays of arrays type is a good substitute.
# use array of array for this
<syntaxhighlight lang="easylang">
proc hashGet ind$ . ar$[][] item$ .
associative$[][] = [ [ 1 "associative" ] [ 2 "arrays" ] ]
for i = 1 to len arrayar$[][]
</syntaxhighlight>
if arrayar$[i][1] = indexind$
And here are functions for indexing associative arrays:
item$ = arrayar$[i][2]
<syntaxhighlight lang="easylang">
break 2return
func indexAssoc index . array[][] item .
for i = 1 to len array[][]
if array[i][1] = index
item = array[i][2]
break 2
.
.
item$ = number "nan"
.
funcproc indexStrAssochashSet indexind$ val$ . arrayar$[][] item$ .
for i = 1 to len arrayar$[][]
if arrayar$[i][1] = indexind$
item$ = arrayar$[i][2] = val$
break 2return
.
.
item$ = ""
.
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
}</syntaxhighlight>
Iterate over keys:
<syntaxhighlight lang="java">for (String key : map) {
{
// the variable "key" is defined here
}</syntaxhighlight>
Iterate over values:
<syntaxhighlight lang="java">for (Int value : map.values) {
{
// the variable "value" is defined here
}</syntaxhighlight>
Iterate over key, value pairs:
<syntaxhighlight lang="java">for ((String key, Int value) : map) {
{
// the variables "key" and "value" are defined here
}</syntaxhighlight>
 
=={{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.
 
<syntaxhighlight lang="easylanginsitux">{
: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="easylang">
 
<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 = h{1: "abc", "1": 789}
 
# 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.new(
key1 => 'value1',
key2 => 'value2',
);
 
# Add a new key-value pair
hash{:key3} = 'value3';</syntaxhighlight>
 
=={{header|Slate}}==
Line 6,404 ⟶ 6,419:
=={{header|Wren}}==
Wren has a Map class built in.
<syntaxhighlight lang="ecmascriptwren">var fruit = {} // creates an empty map
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}}==
885

edits