Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Count number of lines in a STRING

by danj35 (Sexton)
on May 12, 2010 at 09:31 UTC ( #839575=perlquestion: print w/ replies, xml ) Need Help??
danj35 has asked for the wisdom of the Perl Monks concerning the following question:

Good day Monks,

Quick question here, nice and easy. I know how easy it is to count the number of lines in a file, though I am looking to count the number of lines in a string. The string I have can range from having no lines to a theoretically infinite amount. I simply need to count the number of lines in it and return it as a variable (integer). Thanks.

Comment on Count number of lines in a STRING
Re: Count number of lines in a STRING
by moritz (Cardinal) on May 12, 2010 at 09:34 UTC
    You can use the same techniques for counting the number of lines in a string as you do for counting in a file; you just omit the part where you open the file, read the string(s) from the file, and close the file.

    Just operate on the string directly.

    (At least the technique described in perlfaq5 can be easily used that way).

Re: Count number of lines in a STRING
by JavaFan (Canon) on May 12, 2010 at 09:36 UTC
    Assuming lines are defined as sequences of non-newline characters terminated by newlines, it's just a matter of counting the number of newlines. Use the tr/// operator for that.
    my $nr_of_lines = $str =~ tr/\n//;
    Add 1 if you want to count an incomplete last line, and the string doesn't end with a newline.
      Thanks, that's a nice neat bit of working code with the brackets added. Thanks a lot.
      > Add 1 if you want to count an incomplete last line, and the string doesn't end with a newline.

      better use scalar split, counting the delimiter is unnecessary complexity.

      DB<1> $a="1\n2\n3";$b=$a."\n" DB<2> print scalar split /\n/ for $a,$b 33 DB<3> print scalar tr/\n// for $a,$b 23

      Cheers Rolf

        Two problems with scalar split: 1) pre-5.12, it will globber @_. 2) it's incorrect:
        print scalar split /\n/ for "1\n2\n3\n\n"; # Prints 3 instead of 4
        Using a negative third argument doesn't solve the problem:
        print scalar split /\n/, $_, -1 for "1\n2\n3\n\n"; # Prints 5
        you'd have to subtract 1, but only if the string ends in a new line - so you end up looking at the end as well.

        As for the complexity:

        print tr/\n// + !/\n\z/;
        even gives the correct results in list context.
Re: Count number of lines in a STRING
by nagalenoj (Friar) on May 12, 2010 at 10:17 UTC
    TIMTOWTDI

    Here's is the another way.Treating the scalar as a file handle.

    use strict; use warnings; use IO::Scalar; my $string = "one\ntwo\nthree\nfour\nfive\nsix"; my $scalar_fh = IO::Scalar->new( \$string ); # creating a scalar fi +le handle. my $count = 0; $count++ while (<$scalar_fh> ); print $count;

    This is not efficient than using tr. But, you can process a string as file by this way.

Re: Count number of lines in a STRING
by jospan (Novice) on Nov 10, 2010 at 20:57 UTC
    In an AWK-ish kind of way, straight from the command prompt: perl -lne "END { print $. }" myFilename
      Sorry misread the thread: my replies gives the total number of lines in a FILE not in a STRING

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-01 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (8 votes), past polls