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 "");
}