Problem
You want to count the words in a file, using Perl.
Solution
Obviously wc -w can be used for this under UNIX.
But fairly decent demo on the power of split and push! 🙂
Example
while(<>) { push @words, split; }
print "found ".scalar(@words)." words in filen";
Works nicely, even over multiple lines:
[[email protected]]/var/log/httpd% echo "test 1 23 4 n a b c" | perl -e 'while() {
chomp();
push @words, split;
}
print "found ".scalar(@words)." words in filen";
'
found 7 words in file