The problem is that you're not counting the words in the file you're ... um.. To be honest, I'm not quite sure what you are counting. 171 seems to big a value for you to be counting the number of words in the filenames of files in the current directory whos names match one of the lines in your datafile, but I think that is what you are doing.
The problem is this line while(<@source>){.
This isn't iterating over the array @source! It is supplying the contents of @source to the diamond operator <>, which in this context performs a glob on each of its arguments against the current working directory. And you are counting the results of this glob? The number still seems to high ... but whatever. To make your program work you could change to
#!/usr/bin/perl
use strict;
open (SOURCE, "test.txt")||die "Can't open test.txt: $!";
my @source=<SOURCE>;
close (SOURCE);
my $size=0;
for( @source ){
$size++ while m{\b\w+\b}g;
}
print "wordcount: $size words\n";
There are several better ways to count words in files, but that should get you going.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
|