Associative array/Creation: Difference between revisions
Content added Content deleted
(Forth Foundation Library example for forth) |
No edit summary |
||
Line 3: | Line 3: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
Because ActionScript does not have associative arrays in the normal sense, Object objects are used instead and keys are simply properties on those objects. |
Because ActionScript does not have associative arrays in the normal sense, Object objects are used instead and keys are simply properties on those objects. |
||
<actionscript> |
<lang actionscript> |
||
var map:Object = {key1: "value1", key2: "value2"}; |
var map:Object = {key1: "value1", key2: "value2"}; |
||
trace(map['key1']); // outputs "value1" |
trace(map['key1']); // outputs "value1" |
||
Line 13: | Line 13: | ||
map['key3'] = "value3"; |
map['key3'] = "value3"; |
||
trace(map['key3']); // outputs "value3" |
trace(map['key3']); // outputs "value3" |
||
</lang> |
|||
</actionscript> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GNAT|GPL 2007}} |
{{works with|GNAT|GPL 2007}} |
||
<ada> |
<lang ada> |
||
with Ada.Containers.Ordered_Maps; |
with Ada.Containers.Ordered_Maps; |
||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
||
Line 51: | Line 51: | ||
Ada.Text_IO.Put_Line("Yellow:" & Integer'Image(Value)); |
Ada.Text_IO.Put_Line("Yellow:" & Integer'Image(Value)); |
||
end Associative_Array; |
end Associative_Array; |
||
</ |
</lang> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
Line 144: | Line 144: | ||
{{works with|C sharp|C#|3.0+}} |
{{works with|C sharp|C#|3.0+}} |
||
<csharp> |
<lang csharp> |
||
var map = new Dictionary<string, string> {{"key1", "foo"}}; |
var map = new Dictionary<string, string> {{"key1", "foo"}}; |
||
</ |
</lang> |
||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
Line 482: | Line 482: | ||
Hashes are a built-in type called dictionaries (or mappings) in Python. |
Hashes are a built-in type called dictionaries (or mappings) in Python. |
||
<python> |
<lang python> |
||
hash = dict() # 'dict' is the dictionary type. |
hash = dict() # 'dict' is the dictionary type. |
||
hash = dict(red="FF0000", green="00FF00", blue="0000FF") |
hash = dict(red="FF0000", green="00FF00", blue="0000FF") |
||
hash = { 'key1':1, 'key2':2, } |
hash = { 'key1':1, 'key2':2, } |
||
value = hash[key] |
value = hash[key] |
||
</ |
</lang> |
||
Numerous methods exist for the mapping type http://docs.python.org/lib/typesmapping.html |
Numerous methods exist for the mapping type http://docs.python.org/lib/typesmapping.html |
||
<python> |
<lang python> |
||
# empty dictionary |
# empty dictionary |
||
d = {} |
d = {} |
||
Line 512: | Line 512: | ||
for key, value in d.iteritems(): |
for key, value in d.iteritems(): |
||
print key, value |
print key, value |
||
</ |
</lang> |
||
Note: Python dictionary keys can be of any arbitrary "hashable" type. The following contains several distinct key value pairs: |
Note: Python dictionary keys can be of any arbitrary "hashable" type. The following contains several distinct key value pairs: |
||
<python> |
<lang python> |
||
myDict = { '1': 'a string', 1: 'an integer', 1.0: 'a floating point number', (1,): 'a tuple' } |
myDict = { '1': 'a string', 1: 'an integer', 1.0: 'a floating point number', (1,): 'a tuple' } |
||
</ |
</lang> |
||
(Some other languages such as ''awk'' and ''Perl'' evaluate all keys such that numerically or lexically equivalent expressions become identical entries in the hash or associative array). |
(Some other languages such as ''awk'' and ''Perl'' evaluate all keys such that numerically or lexically equivalent expressions become identical entries in the hash or associative array). |
||
Line 525: | Line 525: | ||
(accessible via the ''id()'' built-in function) is commonly used for this purpose. |
(accessible via the ''id()'' built-in function) is commonly used for this purpose. |
||
</ |
</lang> |
||
=={{header|Raven}}== |
=={{header|Raven}}== |