Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Merge 2 strings like a zip [unzip()]

by tel2 (Pilgrim)
on Jul 09, 2015 at 07:08 UTC ( [id://1133895]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Merge 2 strings like a zip [unzip()]
in thread Merge 2 strings like a zip

How dare you put us Kiwis to shame...again, Ken.

Thanks again and keep up the good work!

BTW, what's the 'for' for in:
   print for unzip...

  • Comment on Re^3: Merge 2 strings like a zip [unzip()]

Replies are listed 'Best First'.
Re^4: Merge 2 strings like a zip [unzip()]
by kcott (Archbishop) on Jul 09, 2015 at 14:18 UTC

    You're welcome.

    Regarding the for statement modifier, I think ++hexcoder has covered most of this in his response.

    That's a fairly common Perl idiom that you're likely to see a lot. In fact, my usage wasn't the first in this thread: BrowserUk's zip() code has '... for split '', $b;'.

    There's a bit more going on behind the scenes.

    • for iterates its list, aliasing $_ to each item in turn (see perlsyn: Statement Modifiers for details)
    • print defaults the filehandle to STDOUT and the list to the single item $_ (that's true in this case but an oversimplification nonetheless: see print for the full story)
    • The '-l' switch (on the shebang line) adds a "\n" to the end of each print statement (again, true in this case but still an oversimplification: see perlrun for the full details of '-l')
    print for unzip('AaBbCcDdEeFGHIJ', 5);

    is equivalent to

    for (unzip('AaBbCcDdEeFGHIJ', 5)) { print STDOUT "$_\n"; }

    -- Ken

      Thanks for that explanation, Ken.

      I was aware of what "print for..." does (though I didn't realise some of the finer points you've mentioned), but I asked because I thought your code also worked without the 'for'.  I now notice that the output without the 'for' has no newline between the returned arguments, i.e.:
         ABCDEFGHIJabcde
      which I assume is because it's just printing the 2 arguments (like a list) together.

      All that makes sense now though (I think), thanks.

      Sorry I wasn't clearer with my last question.

        No worries. And, yes, your assumption is correct. Alternatives might be

        print join "\n", unzip('AaBbCcDdEeFGHIJ', 5);

        or the completely mad and hairy

        { local $" = "\n"; print "@{[unzip('AaBbCcDdEeFGHIJ', 5)]}"; }

        which, of course, you'd never consider doing, would you? :-)

        -- Ken

Re^4: Merge 2 strings like a zip [unzip()]
by hexcoder (Curate) on Jul 09, 2015 at 08:09 UTC
    print for unzip('AaBbCcDdEeFGHIJ', 5);

    could be written more explicitly also as

    for (unzip('AaBbCcDdEeFGHIJ', 5)) { print $_; }
    The Perl documentation mentions this here: Statement Modifiers

      print for unzip('AaBbCcDdEeFGHIJ', 5); could be written more explicitly also as for (unzip('AaBbCcDdEeFGHIJ', 5)) { print $_; } The Perl documentation mentions this here: Statement Modifiers

      I think you 're looking for verbosely in lieu of explicitly

      I could be wrong :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found