in reply to (tye)Re: Trying to pass a hash of hash of lists
in thread Trying to pass a hash of hash of lists

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.

</code>

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

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

    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")

      Otherwise we'd have to update a ton of documentation and do s/list/list or array/.

      No, whenever a list is wanted, an array can be used because arrays evaluate to lists in list context. An array can be "downgraded" to list, but a list can never be an array without explicitly creating an array (either through the anonymous array reference constructors ([]) or an array assignment).

      There are places where it is important to distinguish "arrays" from "lists" but perllol.pod is not such a place.

      Of all places, perllol should know better. It's a very technical document, that serves as a tutorial for many people. It's important not to confuse beginners (and experts). String and number are converted on the fly in Perl, but saying that Foo is a number is wrong: it's a strings that evaluates to a number (0) in numeric scalar context (which can only be created by numeric operators). You can use a string as if it were a number, and you can use a number as if it were a string, but numbers and strings will never really be the same. You can use an array as if it were a list, (not the other way around, ) but arrays are not lists.

      I could rant at how wrong you are for using "array" when you can't put an array in an array.

      And right you would be.


      A reference can evaluate to a number. A reference can evaluate to a string. But you can't reverse that: ${ "SCALAR(0x80fd338)" } will never be a real dereference.

      When you have @foo = @bar, @bar is evaluated in list context, and the individual elements are assigned to the new array. That is because the syntax for an array assignment is not ARRAY = ARRAY, but ARRAY = LIST. That's also why copies are made.

      scalar (numeric -- string -- boolean)list
      undef0 -- "" -- false-
      number123 -- "123" -- 0 ? false : true-
      string0 -- "foo" -- "" or "0" ? false : true-
      reference255 -- "TYPE(0xFF)" -- true-
      arraynumber of elementsthe elements themselves
      listlast elementthe elements themselves
      hashnumber of keysa list of keys and values (interleaved)
      Array and list are not the same thing, but you can evaluate arrays in list context ("convert" them).

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk