Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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. thankswhile <FILE> { $total += $_; $count++; } my $average = $total/$count;
|
|---|
| 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 | |
|
Re: Beginner question - understanind a loop with a file handle
by kennethk (Abbot) on Oct 14, 2010 at 21:49 UTC | |
|
Re: Beginner question - understanind a loop with a file handle
by Generoso (Prior) on Oct 15, 2010 at 01:28 UTC | |
by GrandFather (Saint) on Oct 16, 2010 at 06:52 UTC | |
|
Re: Beginner question - understanind a loop with a file handle
by ikegami (Patriarch) on Oct 18, 2010 at 17:07 UTC |