Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-29 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found