Your initial question gave me an opportunity to think through some issues that have been annoying me for some time and for that I am grateful. But I am puzzled by the response above.

  1. In Lisp values are randomly accessible by key via helper functions (assoc or find for alists, get for plists). Are you suggesting that the lack of a syntax to hide the need for helper functions disqualifies them?
  2. The implementation details are irrelevant ... please see my response to tilly: Re^5: Associative array.

  3. Viewing array indices as keys (as opposed to mere access order) is the theoretical basis for claiming that "associative arrays" are a generalization of normal arrays, i.e. they have a more general universe of possible keys. If anyone is doing the confusing, it is whoever first came up with the term "associative array".

    My objection to the term comes from the fact that generalizing the key set also changes the mathematical properties of that key set. And that in turn changes the range of valid operations on those keys and how we work with them on a theoretical and even practical level.

    What operations are different? Well, for one, in a normal array we can meaningfully talk about the distance between elements because integer keys can be subtracted one from another. We can also divide indices (or more precisely the distance between two indices) by some constant. Those two properties play an important role in certain searching and sorting algorithms (binary search and sort for example)

    And then there is ordering. A finite range of integers has a natural well-ordering based on '<='. An arbitrary set of keys only does if we have defined (and applied) a function that maps those keys to the set of positive integers (or some subset). There are many, many ways to do this (locale based, asciibetical). In fact it is mathematically guarenteed that we can find such a function for any finite set. However, this is an additional step that must be done in order to make an associative array behave like a normal array, assuming you even want to do that.

    But often we don't. We use normal arrays and "associative arrays" for very different kinds of algorithms. Associative arrays are best used when our algorithm relies on the semantic significance of each element. Normal arrays are used when our algorithm relies on the order of elements.

    Sorting and reordering only changes what value is associated with what key. It doesn't change the fundamental fact that the values are associated with integral keys and that integers have certain useful mathematical properties. In fact, one of the motivations for using normal arrays in sorting is their ability to map elements to a well ordered set of keys.

  4. Even if Perl is always using bucket order, isn't the net result a different key order? The documentation for keys says (I have bolded the key words):
    The keys are returned in an apparently random order. The actual random order is subject to change in future versions of perl, but it is guaranteed to be the same order as either the values or each function produces (given that the hash has not been modified). Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see "Algorithmic Complexity Attacks" in perlsec).

    What am I misunderstanding there?

Best, beth


In reply to Re^5: Associative array by ELISHEVA
in thread Associative array by gem555

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.