in reply to Output from read/write txt file
use strict; use warnings; open(my $in, "<", "input.txt") or die "Cannot open < input.txt: $!"; open(my $out, ">", "output.txt") or die "Cannot open < output.txt: $!" +; my ($words, $chars) = (0, 0); while (<$in>) { $chars += length($_); $words += scalar(split(/\s+/, $_)); } print $out ("Words=$words\nCharacters=$chars\n");
You might want to chomp if to exclude the newline from the character count.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Output from read/write txt file
by Hayest (Acolyte) on Mar 12, 2015 at 15:29 UTC | |
by toolic (Bishop) on Mar 12, 2015 at 15:34 UTC | |
by Hayest (Acolyte) on Mar 12, 2015 at 15:38 UTC | |
by AppleFritter (Vicar) on Mar 12, 2015 at 21:14 UTC |