Array: Difference between revisions

159 bytes removed ,  14 years ago
Correction: in Perl, "1" and "1.0" are distinct hash keys.
m (→‎C: lang tag / - nbsp)
(Correction: in Perl, "1" and "1.0" are distinct hash keys.)
Line 5:
:The implementation of '''arrays''' is based on setting the bounds of indices of the array, the ''size'' of the array, normally by allocating a contiguous region of memory to hold the elements of the array, and using simple offset calculations on the indices from the origin of the memory to access memory elements. Some languages support extensions to allow such arrays to be resized, or re-shaped, in which the memory area is adjusted, but extent elements are retained.
 
:By contrast an '''[[associative array]]''' maps the association between index "keys" and their associated values, generally using more complex [http://en.wikipedia.org/wiki/Hash_function hash functions] on the keys of the array to map them to their corresponding elements (by pointers, references or memory addresses of some sort). Associative arrays are referred to variously as "hashes" ([[Perl]]), "maps" or "mappings" ([[Lua]]), "dictionaries" ([[Python]]) as well as "associative arrays" ([[AWK]], [[ksh]] and others). The keys into associative arrays are normally not constrained to be integers (unlike arrays which generally required contiguous integer ranges). Different languages may impose various constraints on these keys. For example, in [[Perl]], keys aremust evaluatedalways asbe Perlstrings, so 1, "scalar1", valuesand such1.0, that keyseach of 1which (astringifies literal integer),to "1", (a string representingare the same) key, 1.0 (a literal floating point which is numerically equivalent) andbut "1.0" (anotheris stringdistinct representing a numerically equivalent value) willfrom all mapof to the same key/value in a hashthese. Other languages (such as Python) may treat each type of object as distinct. (See [[associative array]] for further discussion).
 
:Non-associative arrays may have speed and memory consumption advantages. Associative arrays have greater flexibility in types used for keys and generally obviate the need to implement searches through the collection (Each component on which one would search can be implemented as a different associative array of references to their corresponding values or records).
845

edits