elhombre wrote:
> I'm sure this is a typical newbie error but I cannot see why this prints
>
> FILE wordcount is 17 when it should print c:\original.txt word count is
> 17.
>
> If I try
>
> print $_ . " word count is " . $wordCount . "\n";
>
> it complains that $_ is unitialised and I cannot see why.
>
> Any ideas ?
>
>
> foreach (@ARGV) {
> open(FILE, $_) or die "File $_ does not exist";
> while (<FILE>) {
> my @words = split;
> foreach my $word (@words) {
> $wordCount++;
> }
> }
> print FILE . " word count is " . $wordCount . "\n";
> close FILE;
> $wordCount = 0;
> }
made a few mods - this works better (see comments below...
use strict;
foreach (@ARGV) {
my $file = $_;
my ($wordCount,$word);
open(FILE, $file) or die "File $file does not exist";
while (<FILE>) {
my @words = split;
foreach my $word (@words) {
$wordCount++;
}
}
print $file . " word count is " . $wordCount . "\n";
close FILE;
}
Try uncommenting the $file def and replace $_ with $file and see if you
get any different results.
perl g.pl login.com login.com y.com version.com
word count is 370
word count is 370
word count is 14
word count is 63
VS.
perl g.pl login.com login.com y.com version.com
login.com word count is 370
login.com word count is 370
y.com word count is 14
version.com word count is 63