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!
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!