Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Seeking Perl docs about how UTF8 flag propagates

by raygun (Scribe)
on May 19, 2023 at 04:51 UTC ( [id://11152293]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Seeking Perl docs about how UTF8 flag propagates
in thread Seeking Perl docs about how UTF8 flag propagates

Perl has a distinction between a LIST and an ARRAY
Thanks; I've seen both terms, and assumed them to be basically interchangeable—which in a lot of contexts I reckon they are. But I take your point that if split returned an array, you couldn't do something like ($word1, $word2, $word3, $word4) = split(/ /, 'This is a sentence.'). So thanks for the clarification.

Replies are listed 'Best First'.
Re^5: Seeking Perl docs about how UTF8 flag propagates - lists and arrays
by Discipulus (Canon) on May 19, 2023 at 10:26 UTC
    Hello raygun,

    in effect the difference is subtle and can fade into equivalence, but see it from this point of view: a list is not a Perl data type. Perl has three main variable types: scalars, arrays, and hashes. (perlintro).

    In Learning Perl - third edition is stated this way:

    > A list is an ordered collection of scalars. An array is a variable that contains a list. In Perl, the two terms are often used as if they're interchangeable. But, to be accurate, the list is the data, and the array is the variable. You can have a list value that isn't in an array, but every array variable holds a list

    Sometimes, just to make things more foggy, you can do operations on list in the same way as you do with array:

     print +( qw(J A P H) )[2] prints P

    but you cant shift a list and the doc goes: shift ARRAY but for example print accepts a list: print LIST

    Another key concept is LIST in respect of context and here it overlaps with array: LIST or SCALAR context: in list context you can assign it to an array, but again, the array is the perl variable and the list is its value.

    See also: What is the difference between a list and an array? and Scalars, Lists, and Arrays

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Drowning(Distraction) in(via) nomenclature
by parv (Parson) on May 19, 2023 at 09:24 UTC
    if split returned an array, you couldn't do something like ($word1, $word2, $word3, $word4) = split(/ /, 'This is a sentence.')

    What am I missing in ...

    @collect = split( / /, 'This is a sentence.' ); ( $word1, $word2, $word3, $word4 ) = @collect; print join( q[ ; ], ( $word1, $word2, $word3, $word4 );

    ... ?

      What am I missing

      The $word* variables aren't getting assigned "an array", they are getting assigned the values from the array.

      @collect = split( / /, 'This is a sentence.' );
      1. The array on the LHS means that the assigment is in list context.
      2. Split returns a list of scalars.
      3. In list context, the elements of what's on the LHS get assigned to the values from the list on the RHS, so the @collect array is populated from the values returned by split
      ( $word1, $word2, $word3, $word4 ) = @collect;
      1. The (...) on the LHS mean that the assignment is in list context
      2. Per List value constructors, the @collect array gets evaluated in list context, which results in the list (on the RHS) consisting of the elements from the @collect array
      3. The assignment then happens in list context, so the leftmost item of the list on the LHS ($word1) gets assigned the value of the leftmost item from the list on the RHS, and so on
      print join( q[ ; ], ( $word1, $word2, $word3, $word4 ); => syntax error at parv.pl line 3, near ");"

      but with a second ) on that line to allow it to compile:
      1. the words are given list context by the parentheses (unnecessarily, because join already gives list context)
      2. join concatenates the elements of that list, using space-semicolon-space as the separator, into a string
      3. That string is then printed.

      (LHS = "Left-hand side", RHS = "Right-hand side": in these cases, relative to the assignment operator =)

Log In?
Username:
Password:

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

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

    No recent polls found