in reply to Re^2: Associative array
in thread Associative array
Indeed. I put this somewhere in the same category as "lists in scalar context". It fits the orthography and good luck trying to get people to see differently. However, if one views a hash as an associative array one is likely to end up in all sorts of confusion, especially if one is first familiar with Lisp.
Two of the most important sources of confusion are: (a) confusion of implementation with concept and (b) confusion over ordering.
The name "associative array" confuses an implementation of a hash with the concept of a hash. Conceptually, a Perl hash is a data structure that will allow one to retrieve a value by name rather than position. There are many ways to implement this. For example, in Lisp there are two different ways to get hash-like effects: alists and plists.
In many other languages, Perl included, hashes (sometimes referred to as dictionaries) are implemented using data structures that are much more complicated than either a-lists or p-lists. Perl in fact has two entirely different C structures for hashes (HV) and arrays (AV). Although both have ARRAY, FILL, and MAX to describe their memory usage, the similarity stops there. The HV structure is carefully organized to maximize the speed with which values can be retrieved by property name and to conserve storage (in some cases keys are stored as pointers to a shared set of keys rather than having their own private string representation). See PerlGuts Illustrated.
Now, I suppose you could argue that even Perl stores key-value pairs in its ARRAY member and that makes it an associative array. However, that is a misleading oversimplification. And it brings us to the next issue: ordering.
Another problem with viewing hashes as associative arrays is that an array has an explicit ordering to its elements, whether those elements are simple scalars or key-value pairs. Hashes, by contrast, have an undefined unordering. Failing to grasp the distinction can cause serious problems, including hard to track intermittent bugs. One who views a hash as an associative array is likely to assume that ordering of hash members is consistent between runs of programs and may perhaps base their program on that assumption. However, this is a dangerous assumption. Perl hashes have an unpredictable order by intent. One of the techniques to safeguard software from algorithmic complexity attacks is to randomize the order of hash keys, thus making it harder to find a predictable spot to insert dangerous code. See keys and perlsec for further discussion.
Best, beth
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Associative array
by tilly (Archbishop) on Jul 13, 2009 at 09:57 UTC | |
by Anonymous Monk on Jul 13, 2009 at 10:04 UTC | |
by ELISHEVA (Prior) on Jul 13, 2009 at 11:59 UTC | |
by tilly (Archbishop) on Jul 13, 2009 at 19:05 UTC | |
|
Re^4: Associative array
by BrowserUk (Patriarch) on Jul 13, 2009 at 14:34 UTC | |
by ELISHEVA (Prior) on Jul 14, 2009 at 05:46 UTC | |
by BrowserUk (Patriarch) on Jul 14, 2009 at 08:49 UTC |