Re: Counting the # of characters in a multi-line string
- From: mike.wilson8@xxxxxxxxxxx
- Date: 26 Sep 2006 09:58:25 -0700
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).
.
- References:
- Counting the # of characters in a multi-line string
- From: mike . wilson8
- Re: Counting the # of characters in a multi-line string
- From: DJ Stunks
- Counting the # of characters in a multi-line string
- Prev by Date: Re: Standard input Question
- Next by Date: Re: Standard input Question
- Previous by thread: Re: Counting the # of characters in a multi-line string
- Next by thread: Re: Counting the # of characters in a multi-line string
- Index(es):
Relevant Pages
|
|