Re: Counting the # of characters in a multi-line string




DJ Stunks wrote:
mike.wilson8@xxxxxxxxxxx wrote:
I've been taking little baby steps on understanding how to read
multi-line strings as some of you have been seeing in the past week or
so. I've been posting the [DOT multiline strings].

My next goal is to count the total nunber of characters in a string and
the number of chomps that I did.

Code:

while(<IN>)
{
chomp ();
$DOTct = length ($_);
print "$DOTct\n";

last if /.*\]/;
}

What I'm producing is a character count per $_.
Output ex:
4
4
4

What I'm trying to do is total the count and get 12. Do I need to set
a line counter to 0 and increment from $_?

Also, is there a way to count the number of chomps that was done during
the read?

and, while I'm dispensing advice, since you have many questions about
multiline strings....

it's up to you to decide what constitutes a "line". yes, normally a
"line" ends with "\n", but you can control it through the special Perl
variable $/ a.k.a. $INPUT_RECORD_SEPARATOR. (for which the default is
"\n")

You can read about this and other special variables in perlvar.

You will find that sometimes it's quite useful to set this variable to
the null string ('') and read in everything up to consecutive blank
lines with one readline operation. this is occasionally referred to as
"paragraph mode". Or set this variable to undef to read everything in
one fell swoop (called slurping).

Try it out!

HTH,
-jp

Okay thanks will do. I was able to figure out counting the total # of
characters in a multi-line string so I'm good to go (at least for now).

.



Relevant Pages

  • Re: multi-line strings
    ... >>in java, is there a way to have multi-line strings as in c ... >>is this optimized by the compiler to one string or is it added ...
    (comp.lang.java.help)
  • Re: Embedding a newline in a vb.net string constant
    ... Andrew Backer wrote: ... string using a \. ... Just as it isn't possible to have multi-line strings, ...
    (microsoft.public.dotnet.languages.vb)