Associative array: Difference between revisions

Content added Content deleted
m (more tidying)
No edit summary
Line 2: Line 2:
An '''associative array''' is a collection indexed by arbitrary data types, not just small integers. Whereas an [[array]] is typically implemented as many same-sized items stored in a contiguous block of memory, an associative array must be implemented via a more complex data structure, such as a hash table, a list, or some other type of map.
An '''associative array''' is a collection indexed by arbitrary data types, not just small integers. Whereas an [[array]] is typically implemented as many same-sized items stored in a contiguous block of memory, an associative array must be implemented via a more complex data structure, such as a hash table, a list, or some other type of map.


The terminology and semantics of these vary among different programming languages. For example in Perl they are called “''hashes''” (from abbreviating “hash table”, the underlying implementation) while in Python they are called “''dictionaries''” (by analogy to the normal English usage of the term: an indexed reference by which keys (words) are associated with values (definitions)). In Lua they are called “''tables''”.
The terminology and semantics of these vary among different programming languages. For example in Perl and Ruby they are called “''hashes''” (from abbreviating “hash table”, the underlying implementation) while in Python, Objective-C, and Smalltalk they are called “''dictionaries''” (by analogy to the normal English usage of the term: an indexed reference by which keys (words) are associated with values (definitions)). In Lua they are called “''tables''”. In Java, C++, and Go they are called “''maps''”.


The semantics differ as well. While all of these allow the programmer to associate some sort of key with some sort of value they can differ considerably in how they evaluate the key and what sorts of values can be stored.
The semantics differ as well. While all of these allow the programmer to associate some sort of key with some sort of value they can differ considerably in how they evaluate the key and what sorts of values can be stored.