The last number isn't being added because you aren't reading the last number till AFTER you add the $score to $sum.

In other words, when you begin reading the file you're adding $score to $sum before $score even has anything in it. The second line that's read first adds the FIRST score, then sets $score to the second number. And so on. The last number is never added because the loop exits before $sum has a chance to get $score added to it.

Move $sum = $sum + $score to after your split and that should fix it.

In other news, that "float:" stuff you're doing when declaring your variables doesn't do what you think. "float:" is a block name in Perl... it has nothing to do with type declaration. Perl is not a strongly typed language. Variables will be treated as they ought to be treated... numbers as numbers and strings as strings. Most of the time that's The Right Thing, so don't worry about the fact that you can't declare your vars as floats. Because, in most situations, you can't.

Gary Blackburn
Trained Killer


In reply to Re: HELP by Trimbach
in thread Generating statistics from data in file by yabba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.