Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Drowning(Distraction) in(via) nomenclature

by parv (Parson)
on May 19, 2023 at 09:24 UTC ( [id://11152298]=note: print w/replies, xml ) Need Help??


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

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

... ?

Replies are listed 'Best First'.
Re: Drowning(Distraction) in(via) nomenclature
by pryrt (Abbot) on May 19, 2023 at 14:35 UTC
    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://11152298]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found