Hayest has asked for the wisdom of the Perl Monks concerning the following question:
The input.txt file has 3 lines to it, which is why I think Words and Characters is outputting 3 times. This is the output I am getting: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"); }
Words=10 Characters=68 Words=81 Characters=475 Words=116 Characters=684
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Output from read/write txt file
by toolic (Bishop) on Mar 12, 2015 at 15:25 UTC | |
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 |