Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Associative array

by ELISHEVA (Prior)
on Jul 13, 2009 at 06:50 UTC ( [id://779476]=note: print w/replies, xml ) Need Help??


in reply to Associative array

Perl hashes are not associative arraysassociation lists, though I can see how one might think that way if one comes from a Lisp background. Please take a careful look at perldata and perldsc for a better understanding of Perl hashes and how to work with them.

How did you set up @data_list? The code above would only print out a value if you had set up @data_list something like this:

my @data_list = ( { correct => 42 } );

Or maybe you did something like this: my %data_list =(correct=>42)? That does not set up a collection of data pairs even though the fat comma (=>) may make it seem that way. To iterate through the elements of %data_list using a for loop one would need to use keys and do something like this:

foreach my $k (keys %data_list) { my $v = $data_list->{$k}; print "The value for $k is $v\n"; }

@data_list=(correct => 42) doesn't set up discrete pairs either. It merely sets up a flat array with alternating keys and values. To iterate through key value pairs for @data_list one would need to do something like this:

for (my $i=0; $i < $#data_list; $i+=2) { my $k = $data_list[$i]; my $v = $data_list[$i+1]; print "The value for $k is $v\n"; }

Note that in Perl, %data_list and @data_list are two entirely different variables. If the preceding discussion does not clarify things, I think you will need to post more code so we can see how @data_list and/or %data_list is set up.

Best, beth

Update - struck out "associative array" and replaced with "association list" - see Re^5: Associative array for explanation.

Replies are listed 'Best First'.
Re^2: Associative array
by BrowserUk (Patriarch) on Jul 13, 2009 at 07:07 UTC
    Perl hashes are not associative arrays,

    Perhaps you could explain what you see as the difference? (Cos a lot of people are happy to refer to Perl's hashes as 'associative arrays'.)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      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.

      • alists (association lists) are written like this: ((rose . red) (lily . white) (buttercup . yellow)). The Perl equivalent would be (['rose','red'],['lily','white'], ['buttercup', 'yellow']). This is clearly something quite different from what is meant in Perl by a hash. See Association Lists
      • plists (property lists) are written like this: (rose red lily white buttercup yellow lily white). The Perl equivalent would be written nearly identically:qw(rose red lily white buttercup yellow lily white). But again, this is not what Perl considers as a hash. It is merely an alternating sequence of keys and values. See Property Lists

      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

        I would argue that you have completely reversed the proper comparison; calling the data structure a hash confuses implementation and concept, while calling it an associative array focuses on the concept.

        As Wikipedia says, an associative array is an abstract data type that maps a set of keys onto values. The analogy to an array is that arrays associate indexes with values, while associative arrays associate more general keys with values. So it is like an array, but with a more flexible choice of association. (Depending on the language and/or library, keys may even be arbitrary objects.) There are many ways to implement them. In fact the two Lisp examples you gave are valid (albeit inefficient) implementations!

        The term "associative array" is used in many languages. You can see how ubiquitous the term is across languages by googling for associative array. When I did that, the top links were to the Wikipedia page I gave, then articles on PHP, JavaScript, Oracle PL/SQL, PHP again, an entry in a data structures dictionary, an article on C++ compilers, a Perl article, etc. Clearly the term is very well established in the broader programming world.

        By contrast when we say hash we're referring to the implementation. Perl hashes are internally implemented using hash tables. (Common Lisp also has hash tables built in.) However there is no guarantee that Perl's hashes will always be so implemented. Perl has changed hashing algorithms in the past, and theoretically could switch to using a BTree at some point in the future. (Extremely unlikely though.)

        Hm. Sounds like you're trying to start YAPS (Yet Another Perl Shibboleth). You'll excuse me if I do not wish you well with that.

        1. The alists & plists stuff is a read [intentially sic] herring:

          because, (to the best of my very limited Lisp knowledge), neither can perform the fundamental operation of both associative arrays and hashtables: that of random access of values by key.

          To the best of my knowledge, access to both is strictly sequential from the head.

        2. The implementation details are irrelevant:

          because associative array is an abstract datatype defined in terms of its operations. It can be implemented many different ways.

        3. The implicit ordering is a misnomer;

          You are conflating the order in which the data is accessed when iterated, with the ordering of the data itself.

          • Nobody except the rawest newbie expects AA/hashtables to be ordered.

            And when they do, its for the wrong reasons. As LW is reputed to have said: "iterating over the keys of a hash is like clubbing someone to death with a loaded Uzi".

          • Saying that ordinary arrays have implicit ordering is equally a misnomer.

            If you iterate an array in ascending index order; shuffle the contents and again iterate by ascending index, the data (values) will be differently ordered.

          • The pseudo-randomising of the hash seed for security reasons has as much to do with the iteration order of a hash, as the type of lock on a door has to do with the order people will come through it in the morning.

            The hash seed does not determine the order of iteration. That is (and remains) the sequentially ascending sequence of the bucket array (with diversions for non unitary buckets), just as for arrays.

            The hash seed simply determines which bucket the key/value pair is mapped to. Ie. It affects insertion not traversal.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://779476]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-16 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found