I've got a question in my test:
What is wrong with program that counts number of lines and words in file?
open F, $ARGV[0] || die $!;
my @lines = <F>;
my @words = map {split /\s/} @lines;
printf "%8d %8d\n", scalar(@lines), scalar(@words);
close(F);
My conjectures are:
- If file does not exist, program won't tell us about that.
If there are punctuation signs in file, program will count them, for example, in
abc cba , , ,dce
will be five word, but on the other hand
wc
outputs the same result, so it might be considered as correct behavior.- If
F
is a large file, it might be better to iterate over lines and not to dump it intolines
array.
Do you have any less trivial ideas?