Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please clarify type conversion by perl?

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
I have a script that reads in a spool report and extracts fields from each line. I have one field that my debugging shows is being extracted correctly but when I output it to another file using the printf function its value gets changed. I was writing this as a signed decimal field. However, if I change it to a string field, the correct value is maintained.

The incoming/extracted value of field $GLO is 22,902,247.70 and as my code snippets below show, I am stripping out editing characters and white space. Then when it gets written to the output file its value becomes -2004720226 but I cannot determine why. If I debug the content of the field immediately prior to the printf statement the content is correct.

I know that I must be missing something obvious but I cannot seem to locate the true reason. Hopefully some of you perl guru's can peruse the code below and advise what must be incorrect. All comments appreciated. Thanks



# Define Scalars
my @GLC=0; # GL Number Control Field
my $GLCW=""; # GL Control Work Field
my $GLCTG=""; # GL Category Code
my $GLN=""; # GL Account Number
my $GLD=""; # GL Account Description
my $GLT=""; # GL Account Type
my $GLO=0; # GL OAccount Opening Balance


# Extract Fields from this record
($GLN,$GLD,$GLT,$GLO)=unpack("A15xA45x2Ax4A16",$_);

# Assure Opening Balance is NOT null
if (!$GLO) {
$GLO=0;
}
# Strip out editing characters
$GLO=~s/[,.]//g;
$GLO=~s/^\s+//;

# Output Fixed Format GL Account Record
printf OFH ("%12s%s%2s%15s%-45s%s%13s\n", $GLCW,$FT,$GLCTG,$GLN,$GLD,$GLT,$GLO);
 
Hi

I assume, the code you posted is not related to your problem. There you are formatting $GLO as string, so there can not be any problem. However if you format it as decimal number, you will hit a range overflow, because 2290224770 is greater than the maximum storage used by default for integer numbers. ( Note that you removed the decimal separator too, so the numeric value became 100 times greater. ) So just format the value as unsigned decimal :
Code:
[u]  DB<1> [/u]$GLO=2290224770

[u]  DB<2> [/u]printf "%13s\n%13d\n%13u\n",$GLO,$GLO,$GLO
   2290224770
  -2004742526
   2290224770

Feherke.
 
Thanks Feherke.

I do need to retain the sign of the number. So, using your guide of the integer being too large, I inserted use bigint; which helped with my issue.

Thanks for the pointer - B [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top