in reply to Trying to pass a hash of hash of lists

There is no clean way to get that info via the registry so I decided to hard code the info in a hash of hash of lists.

I don't like spreading bad news, but a hash of lists is not really possible. A list of lists is neither.

Lists and arrays are different things. An array is mutable, but lists are not (lists are immutable ("read-only")). Consider:

# A list (1, 2, 3); # An array assignment (syntax: ARRAY = LIST) my @array = (1, 2, 3); # An array @array # The third element of the array $array[2] # The third element of a list (this actually is a slice) (1, 2, 3)[2] # Arrays are mutable, so the following is possible $array[2]++ # Lists are immutable, so the following is impossible (1, 2, 3)[2]++ # A reference to a named array \@array # A reference to an anonymous array [ 1, 2, 3 ] # A reference to a named array that later becomes anonymous my $ref; { my @array = (1, 2, 3); $ref = \@array; } # A list of references \(1, 2, 3) # Equal to \(1, 2, 3) (\1, \2, \3) # A list of arrays ([1, 2, 3], [4, 5, 6], [7, 8, 9]) # A reference to an (anonymous) array of (anonymous) arrays [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
This is the point where most people think "But what about perllol? That talks about lists of lists!". Well, perllol is wrong, sorry.

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Replies are listed 'Best First'.
(tye)Re: Trying to pass a hash of hash of lists
by tye (Sage) on Mar 14, 2002 at 17:39 UTC

    IMO, an array is a list. A list may not be an array. An array is not just a list; it is a special type of list that has extra features. So "list of lists" is just fine, if more vague than necessary since any perl "list of lists" would more precisely be called a "list of references to arrays". But perllorta.pod probably isn't a better name.

    I suppose you could define "list" such that it does not include "array". But that would be a very bad idea, IMO, and would conflict with a lot of the standard Perl documentation.

            - tye (but my friends call me "Tye")

      IMO, an array is a list.

      An array, when used in list context, evaluates to its elements. When used in scalar context, it evaluates to its number of elements. (You'll also notice that there is no "array context", just as there is no "hash context".) Consider:

      # A list (1, 2, 1, 2, 3); # An array @array; # Array assignment (ARRAY = LIST) @array = (1, 2, 1, 2, 3); # List in list context print (1, 2, 1, 2, 3); # 12123 # Array in list context print @array; # 12123 # List in scalar context print scalar (1, 2, 1, 2, 3); # 3 (not 5!!) # Array in scalar context print scalar @array; # 5 (not 3!!)

      So in list context, an array evaluates to the same list that was used in its assignment (unless altered, of course), but in scalar context, a list evaluates to its last element and an array evaluates to its number of elements.

      • You can change an array, you cannot change a list.
      • An array knows its number of elements (MAX in the AV struct, internally), but a list does not.
      • You can reference an array, but you cannot create a reference to a list (instead, a list of scalar references is made).
      • An array is a data type suited for storage, a list is not.
      </code>

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

        Let me try again.

        "List" is used to mean a lot of things in a lot of places. Above you are using "list" specifically to contrast arrays from other types of lists. That is fine and makes sense.

        It is also perfectly valid to use "list" as a more generic term that includes arrays. Otherwise we'd have to update a ton of documentation and do s/list/list or array/. That would just make the documentation more confusing, IMO.

        When you see the term "list" in relation to Perl, you need to figure out what it means in the context at hand. In talking about "list of lists", the term "lists" includes "arrays" and that can make such discussions much simpler and so easier to understand. There are places where it is important to distinguish "arrays" from "lists" but perllol.pod is not such a place.

        I am well aware of what features the concept of "array" adds beyond the concept of "list of scalar values" and I've talked about that quite a bit several times before.

        If you get some narrow definition of "list" stuck in your head such that you can only think of that definition when you see the word "list" used in relation to Perl, then you are going to run into a lot of people using other somewhat different or very different definitions of "list" in limited contexts.

        I could rant at how wrong you are for using "array" when you can't put an array in an array. You can put references to arrays in an array. But it is a good thing to forget some complicating details from time to time and speak somewhat less precisely than is absolutely possible.

                - tye (but my friends call me "Tye")