Hi all, Having problems adding values pulled from a string. I keep getting negative total when there are no negative numbers found in the file. I am sure it has something to do with my use of sprintf. Can someone take a peek at my code and advise where I am going wrong. Much thanks!!!

open(IN,"$inFile"); while(defined($line = (<IN>))){ $key=substr($line,34,1); if($key eq "7"){ $VAL1 = substr($line,137,13); $VAL2 = substr($line,124,13); $VAL3 = substr($line,56,15); $TOT1 = sprintf '%020d',$TOT1+$VAL1 ; $TOT2 = sprintf '%020d',$TOT2+$VAL2 ; $TOT3 = sprintf '%020d',$TOT3+$VAL3 ; #print "$key \n"; } if($key eq "8"){ $VAL4 = substr($line,43,13); $VAL5 = substr($line,124,13); $VAL6 = substr($line,56,15); $TOT4 = sprintf '%020d',$TOT4+$VAL4 ; $TOT5 = sprintf '%020d',$TOT5+$VAL5 ; $TOT6 = sprintf '%020d',$TOT6+$VAL6 ; #print "$key \n"; } $TOT1 = sprintf '%020s',0 if ($TOT1 eq ""); $TOT2 = sprintf '%020s',0 if ($TOT2 eq ""); $TOT3 = sprintf '%020s',0 if ($TOT3 eq ""); $TOT3 = sprintf '%020s',0 if ($TOT4 eq ""); $TOT4 = sprintf '%020s',0 if ($TOT5 eq ""); $TOT5 = sprintf '%020s',0 if ($TOT6 eq ""); }

In reply to math problems by mmittiga17

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.