Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I test for a blank line when reading from a file?

by bravismore
on Oct 11, 2000 at 20:50 UTC ( #36246=perlquestion: print w/replies, xml ) Need Help??

bravismore has asked for the wisdom of the Perl Monks concerning the following question: (files)

How do I test for a blank line when reading from a file?

Originally posted as a Categorized Question.

  • Comment on How do I test for a blank line when reading from a file?

Replies are listed 'Best First'.
Re: How do I test for a blank line when reading from a file?
by merlyn (Sage) on Oct 11, 2000 at 20:54 UTC
    print "it's blank" unless /\S/;
    (This assumes that a line consisting of only whitespace characters is considered blank.)
Re: How do I test for a blank line when reading from a file?
by thanos1983 (Parson) on May 29, 2014 at 21:01 UTC

    Extending runrig's answer to cover the case where "blank" could include whitespace characters (as merlyn's answer already does, negatively): Use /^\s*$/
    As in:

    while (<>) { print "blank line detected\n" if /^\s*$/; }

Re: How do I test for a blank line when reading from a file?
by runrig (Abbot) on Oct 11, 2000 at 20:52 UTC
    If by "blank" you mean "empty", then /^$/, as in:
    while (<>) { print "blank line detected\n" if /^$/; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2021-04-21 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?