Perl line count per line

Using perl, is there any one command that gives me the number of lines inside a line?

my $linenum= .... $str ....

It should work when a line is empty, one line and several lines.

+4
source share
2 answers

Try using regex

-3
source

You can count the number of newline characters \nper line (or \rfor a newline Mac)

my $linenum = $str =~ tr/\n//;
+7
source

All Articles