Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: truth in while condition

by johngg (Canon)
on Sep 30, 2018 at 11:19 UTC ( #1223319=note: print w/replies, xml ) Need Help??


in reply to truth in while condition

You haven't chomped the newline.

johngg@shiraz:~/perl/Monks$ perl -E ' $_ = qq{0\n}; say $_ ? q{True} : q{False}; chomp; say $_ ? q{True} : q{False};' True False

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: truth in while condition
by TISON (Acolyte) on Sep 30, 2018 at 11:33 UTC
    Thanks for your reply! Sounds reasonable but what do you think about comment by LanX? It seems they are technically different.
      Both are right, but it's a bit tricky to construct a while-condition doing a readline and exiting at false.

      I'd rather use an explicit last with condition inside the body.

      use strict; use warnings; use feature 'say'; while ( $_= <DATA>, chomp, $_) { say; } __DATA__ 3 1 0 2
      -->
      3 1

      (NB: Iterators don't necessarily loop over lines where they need to chomp, <update see here>)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Get it! Again thanks to the help of both of you~

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2019-07-24 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (32 votes). Check out past polls.

    Notices?