in reply to Re: Re: array element seperators
in thread array element seperators

...because $, applies to a list following a print statement, not just to an array in a print statement. But be careful. This can be handy but can also produce unexpected results:
my @items = qw(a b c d e); $, = '||'; print @items, "\n"; # Prints: a||b||c||d||e||
Which may not be what you wanted if you forgot that the "\n" became an additional item in the flattened list after the print.

Replies are listed 'Best First'.
Re: Re: Re: Re: array element seperators
by sierrathedog04 (Hermit) on Mar 11, 2001 at 22:53 UTC
    dvergin wrote:
    because $, applies to a list following a print statement, not just to an array in a print statement.

    But aren't all lists arrays, and all arrays (other than associative arrays) lists? I thought that the two terms, arrays and lists, were more or less synonymous except for the aforementioned associative arrays a.k.a. hashes.

      But aren't all lists arrays, and all arrays ... lists?

      This can get tricky. And there are sometimes strong opinions expressed on various aspects of this question -- not all of them in agreement. But on one thing there is agreement: "a list value is different from an array." Programming Perl, 3rd Edition, p. 73.

      Consider this:

      print scalar ('a','b','c','d'); # prints 'd' my @array = ('a','b','c','d'); print scalar @array; # prints '4'
      Some will argue that the first example does not involve a list, but rather the comma operator doing its scalar context thing (i.e. evaluating and throwing away the result of each value on the left (in turn) and then evaluating and returning the value on the right -- ultimately, the last one).

      Others suggest that it is a 'list literal' (as opposed to an array) but that in a scalar context it is not behaving like a true list (written 'LIST' in Prog. Perl 3). As you can see these distinctions are not for the faint of heart.

      It may be helpful to think of tacking 'literal' before the word 'list'. From that it becomes clearer that, for example: a (literal) list is stable in length while an array can be pushed or popped or changed in other ways. It may even help you make sense of the fact that, in a scalar context, arrays return the number of elements while (literal) lists return the last element of the list.

      In a (literal) list, the comma operator is present and can do its thing. In an array, the comma operator that may have helped form it is now gone and we are left with a series of values accessable through the array variable.

      All of which invites the question:

      my @array1 = ('a','b'); print scalar ('x','y',@array1);
      What does this print? Answer: '2' This is the last item in the literal list (i.e. @array1) which is then evaluated in a scalar context returning the number of elements in @array1. Notice that in the treatment of the literal list, the contents of @array1 were not flattened into the list as they would have been in a true list context (confused yet?) but rather, they were treated as a literal item returned by the comma operator and then evaluated to see what value to return.

      Again, a list is a literal. An array is a variable.

      And don't even ask why

      print scalar ('a','b','c'); print scalar qw(a b c);
      both print 'c' even though the qw(...) example contains no comma operators. Answer: because qw(...) is defined as returning a literal list (including the commas)... not an array!

      You can find this and more on this subject here (brief and recommended) or in Programming Perl, 3rd Edition page 72-74 (longer and also helpful... or confusing... depending on how much sleep you had the night before).

      Update:Read on below and take my bit above about 'literal' with a grain of salt.

        ...and then there is my take: There are so many types of "lists" in Perl that to say anything definitive about a "list" is usually a mistake.

        Most agree that an array is a type of list but that a list need not necessarily be an array.

        The rest of the arguments are a bit silly to me now. They consist of different people trying to precisely define what a "Perl list" is and then arguing because very listish things like ('a','b','c') don't meet some of these definitions.

        For example, the Perl documentation talks about the "list of characters" between square brackets composing a character class.

        See (tye)Re2: tr doesn't *really* use a list and follow-ups for more of my wacky ideas. (:

                - tye (but my friends call me "List")
        Thanks, tye and tilly, for putting my attempt in the context of the larger discussion. (I did search around for precedent posts to cite but got only looong lists of hits that seemed to be about other issues.)

        Tilly, I am particularly struck by the following, cribbed and expanded upon from your post:

        sub ret_array { return @_; } sub ret_list { return @_[0..$#_]; } my @array = ('a','b','c'); print scalar ret_array('a','b','c'); # 3 print scalar ret_array(@array); # 3 print scalar ret_list('a','b','c'); # c print scalar ret_list(@array); # c