At 10:44 pm -0500 25/12/04, Lola Lee wrote:

This script has you count words in a file. The line where one is supposed to read in the file being counted is like so:

while (defined($line = <>))

However, when I type this in: perl countwords.pl history.txt

Nothing happens.


The line you quote simply puts each line of a putative file into the scalar variable $line. What "happens" will depend on how you deal with $line and where you print your results.

Supposing that countwords.pl is a script something like the one below and that history.txt is a file in the same directory (say your home directory), then you will get output in the terminal as below when you run your command. I personally would substitute for your line:

  foreach $line (<>)

which does the same thing and, to me at least, is simpler and clearer.


################### #!/usr/bin/perl foreach (@ARGV) { while (defined ($line = <>)) { @words = split /\s+/, $line; $wordcount = @words; print "$wordcount, "; $total += $wordcount; } print "\n$_....Total: $total \"words\"\n"; } ###################

_TERMINAL_
eremita:~ jd$ cd
eremita:~ jd$ perl countwords.pl histoire.html
7, 2, 4, 2, 4, 6, 5, 4, 2, 6, 2, 0, 1, 1, 0, 3, 1, 1,
histoire.html....Total: 51 words
eremita:~ jd$



.

Reply via email to