Perl Script to count words / lines

I am learning PERL for the first time, and I am trying to reproduce just a simple Perl script on page 4 of this:

This is my code:

# example.pl, introductory example # comments begin with the sharp sign # open the file whose name is given in the first argument on the command # line, assigning to a file handle INFILE (it is customary to choose # all-caps names for file handles in Perl); file handles do not have any # prefixing punctuation open(INFILE,$ARGV[0]); # names of scalar variables must begin with $ $line_count - 0; $word_count - 0; # <> construct means read one line; undefined response signals EOF while ($line - <INFILE>) { $line_count++; # break $line into an array of tokens separated by " ", using split() # (array names must begin with @) @words_on_this_line - split(" ",$line); # scalar() gives the length of an array $word_count += scalar(@words_on_this_line); } print "the file contains ", $line_count, "lines and ", $word_count, " words\n"; 

and this is my text file:

 This is a test file for the example code. The code is written in Perl. It counts the amount of lines and the amount of words. This is the end of the text file that will be run on the example code. 

I am not getting the correct result, and I am not sure why. My way out:

 C:\Users\KP\Desktop\test>perl example.pl test.txt the file contains lines and words 
+4
source share
4 answers

For some reason, all your "=" statements look like a "-"

 $line_count - 0; $word_count - 0; ... while ($line - <INFILE>) { ... @words_on_this_line - split(" ",$line); 

I would recommend using “my” to declare your variables, and then “use strict” and “use warnings” to help you spot typos like this:

Currently

 $i -1; 

/tmp/test.pl - no output

When you add strict warnings and warnings:

 use strict; use warnings; $i -1; 

/tmp/test.pl The global character "$ i" requires an explicit package name in /tmp/test.pl line 4. The execution of /tmp/test.pl was canceled due to a compilation error.

When you add "mine" to declare it:

 vim /tmp/test.pl use strict; use warnings; my $i -1; 

/tmp/test.pl Useless use of subtraction (-) in a void context with /tmp/test.pl line 4. Using an uninitialized value when subtracting (-) with /tmp/test.pl line 4.

And finally, with "=" instead of "-" typo - this is what the correct declaration and initializatoin look like:

 use strict; use warnings; my $i = 1; 
+6
source

You need to change - by = in several sentences in your code. In addition, I have included some changes related to getting more modern perl code ( use strict it should)

 use strict; use warnings; open my $INFILE, '<', $ARGV[0] or die $!; # names of scalar variables must begin with $ my $line_count = 0; my $word_count = 0; # <> construct means read one line; undefined response signals EOF while( my $line = <$INFILE> ) { $line_count++; # break $line into an array of tokens separated by " ", using split() # (array names must begin with @) my @words_on_this_line = split / /,$line; # scalar() gives the length of an array $word_count += scalar(@words_on_this_line); } print "the file contains ", $line_count, "lines and ", $word_count, " words\n"; close $INFILE; 
+3
source

replace while ($line - <INFILE>) {

with while ($line = <INFILE>) {

+2
source

The word counter part can be made a little simpler (and more efficient). Split returns numeric elements if called in a scalar context.

replace

 my @words_on_this_line = split / /,$line; $word_count += scalar(@words_on_this_line); 

with

 $word_count += split / /,$line; 
+1
source

Source: https://habr.com/ru/post/1482036/


All Articles