Associative array/Creation: Difference between revisions

m
Restored alphabetical order of languages
(→‎{{header|Wart}}: Added Vim Script)
m (Restored alphabetical order of languages)
Line 203:
When a list is organized as pairs, the '''lookup in pairs''' block can be used to retrieve an associated value from a key name.<br>
[https://lh3.googleusercontent.com/-Cxw_-XGMRyM/UutOX1bEH9I/AAAAAAAAJ9g/MZotfuSEziY/s1600/CreateIterateLookup.PNG '''<VIEW BLOCKS AND ANDROID APP>''']
 
=={{header|AutoHotkey}}==
=== True arrays ===
[[AutoHotkey_L]] has [http://ahkscript.org/docs/Objects.htm Objects] which function as associative arrays.
<lang AutoHotkey>associative_array := {key1: "value 1", "Key with spaces and non-alphanumeric characters !*+": 23}
MsgBox % associative_array.key1
. "`n" associative_array["Key with spaces and non-alphanumeric characters !*+"]</lang>
=== Legacy versions ===
[[AutoHotkey_Basic]] does not have typical arrays.
However, variable names can be concatenated, simulating associative arrays.
<lang AutoHotkey>arrayX1 = first
arrayX2 = second
arrayX3 = foo
arrayX4 = bar
Loop, 4
Msgbox % arrayX%A_Index%</lang>
 
=={{header|AWK}}==
Line 222 ⟶ 238:
print ( "blue" in a ) # print 1
}</lang>
 
=={{header|AutoHotkey}}==
=== True arrays ===
[[AutoHotkey_L]] has [http://ahkscript.org/docs/Objects.htm Objects] which function as associative arrays.
<lang AutoHotkey>associative_array := {key1: "value 1", "Key with spaces and non-alphanumeric characters !*+": 23}
MsgBox % associative_array.key1
. "`n" associative_array["Key with spaces and non-alphanumeric characters !*+"]</lang>
=== Legacy versions ===
[[AutoHotkey_Basic]] does not have typical arrays.
However, variable names can be concatenated, simulating associative arrays.
<lang AutoHotkey>arrayX1 = first
arrayX2 = second
arrayX3 = foo
arrayX4 = bar
Loop, 4
Msgbox % arrayX%A_Index%</lang>
 
=={{header|Babel}}==
Line 246:
("bar" 42)
("baz" 77)]</lang>
 
=={{header|Batch File}}==
This is cheating, I'm sure of it.
 
<lang dos>::assocarrays.cmd
@echo off
setlocal ENABLEDELAYEDEXPANSION
set array.dog=1
set array.cat=2
set array.wolf=3
set array.cow=4
for %%i in (dog cat wolf cow) do call :showit array.%%i !array.%%i!
set c=-27
call :mkarray sicko flu 5 measles 6 mumps 7 bromodrosis 8
for %%i in (flu measles mumps bromodrosis) do call :showit "sicko^&%%i" !sicko^&%%i!
endlocal
goto :eof
 
:mkarray
set %1^&%2=%3
shift /2
shift /2
if "%2" neq "" goto :mkarray
goto :eof
 
:showit
echo %1 = %2
goto :eof
</lang>
 
Output:
<pre>array.dog = 1
array.cat = 2
array.wolf = 3
array.cow = 4
"sicko&flu" = 5
"sicko&measles" = 6
"sicko&mumps" = 7
"sicko&bromodrosis" = 8</pre>
 
 
=={{header|BASIC256}}==
Line 403 ⟶ 363:
e endive
c carrot</pre>
 
=={{header|Batch File}}==
This is cheating, I'm sure of it.
 
<lang dos>::assocarrays.cmd
@echo off
setlocal ENABLEDELAYEDEXPANSION
set array.dog=1
set array.cat=2
set array.wolf=3
set array.cow=4
for %%i in (dog cat wolf cow) do call :showit array.%%i !array.%%i!
set c=-27
call :mkarray sicko flu 5 measles 6 mumps 7 bromodrosis 8
for %%i in (flu measles mumps bromodrosis) do call :showit "sicko^&%%i" !sicko^&%%i!
endlocal
goto :eof
 
:mkarray
set %1^&%2=%3
shift /2
shift /2
if "%2" neq "" goto :mkarray
goto :eof
 
:showit
echo %1 = %2
goto :eof
</lang>
 
Output:
<pre>array.dog = 1
array.cat = 2
array.wolf = 3
array.cow = 4
"sicko&flu" = 5
"sicko&measles" = 6
"sicko&mumps" = 7
"sicko&bromodrosis" = 8</pre>
 
=={{header|BBC BASIC}}==
Line 546 ⟶ 545:
{{works with|C sharp|C#|3.0+}}
<lang csharp>var map = new Dictionary<string, string> {{"key1", "foo"}};</lang>
 
=={{header|Clojure}}==
<lang lisp>{:key "value"
:key2 "value2"
:key3 "value3"}</lang>
 
=={{header|ColdFusion}}==
<lang cfm><cfset myHash = structNew()>
<cfset myHash.key1 = "foo">
<cfset myHash["key2"] = "bar">
<cfset myHash.put("key3","java-style")></lang>
 
In ColdFusion, a map is literally a java.util.HashMap, thus the above 3rd method is possible.
 
=={{header|Common Lisp}}==
<lang lisp>;; default :test is #'eql, which is suitable for numbers only,
;; or for implementation identity for other types!
;; Use #'equalp if you want case-insensitive keying on strings.
 
(setf my-hash (make-hash-table :test #'equal))
(setf (gethash "H2O" my-hash) "Water")
(setf (gethash "HCl" my-hash) "Hydrochloric Acid")
(setf (gethash "CO" my-hash) "Carbon Monoxide")
 
;; That was actually a hash table, an associative array or
;; alist is written like this:
(defparameter *legs* '((cow . 4) (flamingo . 2) (centipede . 100)))
;; you can use assoc to do lookups and cons new elements onto it to make it longer.</lang>
 
=={{header|Chapel}}==
Line 617 ⟶ 588:
arr2 keys: {John, Pete}
arr2 values: 3 14
 
=={{header|Clojure}}==
<lang lisp>{:key "value"
:key2 "value2"
:key3 "value3"}</lang>
 
=={{header|ColdFusion}}==
<lang cfm><cfset myHash = structNew()>
<cfset myHash.key1 = "foo">
<cfset myHash["key2"] = "bar">
<cfset myHash.put("key3","java-style")></lang>
 
In ColdFusion, a map is literally a java.util.HashMap, thus the above 3rd method is possible.
 
=={{header|Common Lisp}}==
<lang lisp>;; default :test is #'eql, which is suitable for numbers only,
;; or for implementation identity for other types!
;; Use #'equalp if you want case-insensitive keying on strings.
 
(setf my-hash (make-hash-table :test #'equal))
(setf (gethash "H2O" my-hash) "Water")
(setf (gethash "HCl" my-hash) "Hydrochloric Acid")
(setf (gethash "CO" my-hash) "Carbon Monoxide")
 
;; That was actually a hash table, an associative array or
;; alist is written like this:
(defparameter *legs* '((cow . 4) (flamingo . 2) (centipede . 100)))
;; you can use assoc to do lookups and cons new elements onto it to make it longer.</lang>
 
=={{header|Component Pascal}}==
Line 1,432 ⟶ 1,431:
at[4]=four
</pre>
=={{header|Objective-C}}==
{{works with|Cocoa}} and {{works with|GNUstep}}
 
You can use a NSDictionary to create an immutable hash. A dictionary can contain only objects; if you want store non objects like integer, you have to box it in NSNumber.
<lang objc>NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Joe Doe", @"name",
[NSNumber numberWithUnsignedInt:42], @"age",
[NSNull null], @"extra",
nil];</lang>
 
The same as the above with the new literal syntax in clang 3.1+ / Apple LLVM Compiler 4.0+ (XCode 4.4+) :
<lang objc>NSDictionary *dict = @{
@"name": @"Joe Doe",
@"age": @42,
@"extra": [NSNull null],
};</lang>
 
To create a mutable dictionary, use NSMutableDictionary:
<lang objc>NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Joe Doe" forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:42] forKey:@"age"];</lang>
 
You can access value with objectForKey:. If a key does not exists, nil is returned.
<lang objc>NSString *name = [dict objectForKey:@"name"];
unsigned age = [dict objectForKey:@"age"] unsignedIntValue];
id missing = [dict objectForKey:@"missing"];</lang>
 
=={{header|Objeck}}==
Line 1,489 ⟶ 1,462:
map->Find("seven")->As(IntHolder)->GetValue()->PrintLine();
</lang>
 
=={{header|Objective-C}}==
{{works with|Cocoa}} and {{works with|GNUstep}}
 
You can use a NSDictionary to create an immutable hash. A dictionary can contain only objects; if you want store non objects like integer, you have to box it in NSNumber.
<lang objc>NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Joe Doe", @"name",
[NSNumber numberWithUnsignedInt:42], @"age",
[NSNull null], @"extra",
nil];</lang>
 
The same as the above with the new literal syntax in clang 3.1+ / Apple LLVM Compiler 4.0+ (XCode 4.4+) :
<lang objc>NSDictionary *dict = @{
@"name": @"Joe Doe",
@"age": @42,
@"extra": [NSNull null],
};</lang>
 
To create a mutable dictionary, use NSMutableDictionary:
<lang objc>NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Joe Doe" forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:42] forKey:@"age"];</lang>
 
You can access value with objectForKey:. If a key does not exists, nil is returned.
<lang objc>NSString *name = [dict objectForKey:@"name"];
unsigned age = [dict objectForKey:@"age"] unsignedIntValue];
id missing = [dict objectForKey:@"missing"];</lang>
 
=={{header|OCaml}}==