Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Splitting in while loop

by jwkrahn (Abbot)
on Oct 07, 2021 at 07:21 UTC ( [id://11137291]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Splitting in while loop
in thread Splitting in while loop

while (split(/[, ]+/, <DATA>))

Is interpreted by perl as:

while ( @_ = split( /[, ]+/, <DATA> ) )

Replies are listed 'Best First'.
Re^4: Splitting in while loop
by tel2 (Pilgrim) on Oct 08, 2021 at 00:34 UTC
    Thanks jwkrahn, but if they're interpreted to be the same, then why does this code:
    while (split(/[, ]+/, <DATA>)) { print @_ }
    print nothing, while this code:
    #while (split(/[, ]+/, <DATA>)) # Edit: Sorry - ignore this while (@_ = split(/[, ]+/, <DATA>)) # Edit: I meant this { print @_ }
    prints:
    me@here.com those@there.comothers@there.com you@there.comthem@there.com
    ?

    Update: Cancel the above.  I see AnomalousMonk's answer to it now.

      If you just print an array (as in print @_) there will be nothing to show where each element starts and ends:

      $ perl -e '@_ = qw{a bcd ef}; print @_' abcdef

      If you put the array in interpolating quotes, you will see each element separated by the value of the special variable $" (which, by default, is a space):

      $ perl -e '@_ = qw{a bcd ef}; print "@_"' a bcd ef $ perl -e '@_ = qw{a bcd ef}; print qq{@_}' a bcd ef

      In rare situations, perhaps where $" was changed elsewhere in the code, you may want to make a localised change to $" (typically within an anonymous block). See local, "Temporary Values via local()" and "Localization of special variables" for more details on that. Some examples:

      $ perl -e '@_ = qw{a bcd ef}; { local $" = " "; print "@_" }' a bcd ef $ perl -e '@_ = qw{a bcd ef}; { local $" = "__"; print "@_" }' a__bcd__ef $ perl -e '@_ = qw{a bcd ef}; { local $" = "\n"; print "@_" }' a bcd ef

      — Ken

        If you just print an array (as in print @_) there will be nothing to show where each element starts and ends

        Technically, there will be something: the value of the special variable $, -- it just happens that it defaults to undef (‡) so the separator used is nothing when printed. But if you change or localize its value to a different value, there will be a separator, without having to stringify the array first.

        I wouldn't have mentioned this, but you went on to encourage the use of $" and interpolating the array in a string. If all you are doing is trying to get a separator for printing an array or other list (²), then $, will work without having to interpolate/stringify.

        -- pryrt


        ‡: ... and apparently ignores use warnings 'uninitialized';: gotta love magic variables.

        ²: However, $, applies to everything when printing in list context, so local $,=',';print $a, @_, $b will have more separator instances than local $,=undef; local $"=','; print $a, "@_", $b; -- which may or may not be what you want.

        ³: Also, $, is much easier to use in a Windows cmd.exe one-liner than $". As a workaround, use English to the rescue:

        C:\usr\local\share>perl -MEnglish -wle "@_ = qw{a bcd ef}; print qq(@_ +); local $LIST_SEPARATOR=','; print qq(@_)" a bcd ef a,bcd,ef
        Fair point, Ken, but I was just trying to concisely demonstrate that there was stuff-all in @_ with one bit of code, and the addresses in the other bit, so the 2 bits of code could not be equivalent.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-20 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found