Jump to content

Associative array/Creation: Difference between revisions

(→‎Insitux: implementation)
 
(4 intermediate revisions by 3 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 indexAssochashGet indexind$ . arrayar$[][] 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
proc indexAssoc index . array[][] item .
for i = 1 to len array[][]
if array[i][1] = index
item = array[i][2]
break 2
.
.
item$ = number "nan"
.
proc 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 3,932 ⟶ 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,034 ⟶ 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,418 ⟶ 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,447 ⟶ 6,448:
null
</pre>
 
 
=={{header|XLISP}}==
885

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.