Click to See Complete Forum and Search --> : Counting Instances of punctuation in code


barkley1979
05-26-2005, 05:01 PM
I have managed to piece together the following:


open(INPUT, "text.txt") || die "Couldn't Open file: $!";

while ( <INPUT> )
{
my $commacount = 0;
while ($_ =~ /,/g)
{
$commacount++;
}


print("$commacount commas\n");

}

The output is:

3 commas
0 commas


I cannot understand why the "0 commas" entry appears. Is it a simple explanation?

Also how would I amend this to include both a - and a , count?
I have tried the following, but this just confuses me further:

open(INPUT, "text.txt") || die "Couldn't Open file: $!";

while ( <INPUT> )
{
my $commacount = 0;
while ($_ =~ /,/g)
{
$commacount++;
}
print("$commacount commas\n");
}

seek(INPUT,0,0);

while ( <INPUT> )
{
my $hyphencount = 0;
while ($_ =~ /-/g)
{
$hyphencount++;
}

print("$hyphencount hyphens\n");

}

The output is now
3 commas
0 commas
0 hyphens
4 hyphens

Argh!

Jeff Mott
05-26-2005, 06:32 PM
I cannot understand why the "0 commas" entry appears. Is it a simple explanation?Most likely there is some extra white space on the last line of your file. Your code counts the commas on this line as well and finds none.Also how would I amend this to include both a - and a , count?You can modify the regular expression to match both.while ($_ =~ /[-,]/g)The square brackets create a character class. So that effectively reads as "match any hypen or comma."

barkley1979
05-26-2005, 06:46 PM
That's cracked it!
Thanks very much.

Nedals
05-26-2005, 08:19 PM
Your code, as written will get a seperate count for each line in the file.
If you want all the commas in the file, rewrite the script like this..


my $commacount = 0;
my $hyphencount = 0;
open(INPUT, "text.txt") || die "Couldn't Open file: $!";

while ( <INPUT> ) {
while ($_ =~ /,/g) { $commacount++; }
while ($_ =~ /-/g) { $hyphencount++; }
}

print("$commacount commas\n$hyphencount hyphens\n");

CyCo
05-27-2005, 04:41 PM
...just thought I'd throw this out there, since it could be a simple solution using the transliteration operator when one needs only to count ONE (1) character...


open INPUT,'text.txt' or die $!;
while($_ = <INPUT>) {
my $commacount = $_ =~ tr/,//;
printf "$commacount comma%s\n",
$commacount == 1
? ''
: 's';
}
close INPUT;

Charles
05-27-2005, 05:42 PM
#!c:\perl\bin\perl.exe

use strict;

# set the end-of-line delimeter to end-of-file
$/ = undef;

# read the whole file into one scalar
my $slurp = <DATA>;

# generate a list of all of the pundctuation found
# the "s" option makes the regex work across end of line characters
my @punctuation = $slurp =~ m|[,-]|sg;

# in the scalar context a list returns the number of its elements
print scalar @punctuation;

__DATA__
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec in odio ut odio
pellentesque pretium. Etiam commodo lectus sed erat. Suspendisse non sapien eget
diam luctus bibendum. Nunc massa quam, faucibus sed, consequat at, nonummy in,
ante. Nullam ac leo vitae mauris ullamcorper aliquet. Nulla pulvinar nunc nec
magna. Donec non velit ut lorem eleifend luctus. Fusce malesuada dui at sem.
Nunc in odio quis elit rhoncus posuere. Ut augue ante, tempus consequat,
pharetra eu, sollicitudin nec, eros. Etiam porta porta sem. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit.