Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello I'm learning Perl and have a very basic question about a code snippet I don't understand in the book I'm reading. Here is the code
while <FILE> { $total += $_; $count++; } my $average = $total/$count;
This is simply reading in from a filehandle previously created. There is no context in the book given about this bit of code. I think the books assumes it is self-explanatory. I don't understand the ' $total' line. I thought the $_ variable would contain a line of input from the filehandle in each round of the loop. If this input was text and was evaluated in a scalar context it would be converted to 0 and so the total would always be 0? I think this code could make sense though if each line of input in the file was a number. I appreciate this is a very basic question but I would like to check I understand what is going on. I have no other peers to ask. thanks

Replies are listed 'Best First'.
Re: Beginner question - understanind a loop with a file handle
by Corion (Patriarch) on Oct 14, 2010 at 21:47 UTC

    If your files contains numbers (as in, sequences of digits, 0-9), then the numbers will be treated by Perl as numbers:

    1 2 3 4 5

    Then, the variable names will make sense, $total will contain the sum of all numbers, while $count will contain the number of lines in the file.

    What you (or the book) might be missing is an explanation that Perl does not really discriminate between strings and numbers and will transparently convert from one to the other. Only strings that don't look like a number will get converted to 0.

Re: Beginner question - understanind a loop with a file handle
by kennethk (Abbot) on Oct 14, 2010 at 21:49 UTC
    You are correct that the special variable $_ will sequentially contain each line of the file as you move through. These lines will be scalar strings. When a scalar is used in numeric context, Perl will attempt to convert that scalar into a number. If the leading non-whitespace characters look like a number (e.g. '    56th finisher'), you will get a non-zero result (56 in this case).
Re: Beginner question - understanind a loop with a file handle
by Generoso (Prior) on Oct 15, 2010 at 01:28 UTC
    # Read each line of file supposing a number per line # while <FILE> { # # the += operator will add each number read to #the accumulated # $total += $_; # # $_ contains the line just read of FILE # ++ will increment 1 for each line giving a total # count of lines at the end of the loop # $count++; } # # at the end of the loop you have total = sum of lines # and count how many lines where read # my $average = $total/$count; # # the formula above calculates the average

    Now Can you see the code? Any line that starts with # is a comment.

      Damn, I'm sure there is supposed to be code in there some where, but for the life of me I can't see it!

      True laziness is hard work
Re: Beginner question - understanind a loop with a file handle
by ikegami (Patriarch) on Oct 18, 2010 at 17:07 UTC

    I think this code could make sense though if each line of input in the file was a number.

    That's exactly what's expected. You understand what's going on perfectly.

    [ I wrote this earlier, but it went unposted. I might as we post it anyway ]