mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:
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 ""); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: math problems
by ikegami (Patriarch) on Mar 28, 2011 at 22:04 UTC | |
Re: math problems
by kennethk (Abbot) on Mar 28, 2011 at 22:11 UTC | |
Re: math problems
by BrowserUk (Patriarch) on Mar 28, 2011 at 22:24 UTC | |
by kennethk (Abbot) on Mar 29, 2011 at 14:41 UTC | |
by BrowserUk (Patriarch) on Mar 29, 2011 at 15:07 UTC | |
by kennethk (Abbot) on Mar 29, 2011 at 15:14 UTC | |
Re: math problems
by apl (Monsignor) on Mar 29, 2011 at 13:04 UTC |
Back to
Seekers of Perl Wisdom