For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > Counting the # of characters in a multi-line string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Counting the # of characters in a multi-line string
mike.wilson8@comcast.net

2006-09-26, 6:57 pm

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 w 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?

Thank you,
Mike

DJ Stunks

2006-09-26, 6:57 pm

mike.wilson8@comcast.net 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 w 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?


help us help you, Mike. that is, for best results, post a short, but
complete script which shows us what you have tried. complete means:

1) the shebang line
2) use strict;
3) use warnings;
4) supply any sample data either as a variable in the script or
in the special __DATA__ filehandle

these pieces of information will help you help yourself and help us
understand exactly what you're trying to do and what is causing
whatever issue it is you're seeing.

-jp

DJ Stunks

2006-09-26, 6:57 pm

mike.wilson8@comcast.net 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 w 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

mike.wilson8@comcast.net

2006-09-26, 6:57 pm


DJ Stunks wrote:
> mike.wilson8@comcast.net wrote:
>
> 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).

usenet@DavidFilmer.com

2006-09-26, 6:57 pm

mike.wilson8@comcast.net wrote:
> My next goal is to count the total nunber of characters in a string


perldoc -q count

How can I count the number of occurrences of a substring
within a string?

> and the number of chomps that I did.


my $number_of_chomps = chomp;

but chomp only removes characters at the end of a string (or at the end
of all strings in an array). It won't remove newlines embedded within
a string (use /s\n//g).

--
David Filmer (http://DavidFilmer.com)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com