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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help (2nd set of eyes) with printf function trouble? 1

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
I am having an issue with one of the output fields occasionally being written at 46 bytes instead of the stated 45 bytes when using the printf function.

I am hoping that another set of eyes can identify what I have been missing. I have read through the perldoc -f sprintf to verify my parameters (doesn't mean I can't miss something) and it "appears" that the format is correct.

Here are a couple of actual input records:

1.111.25.6 BR# 6 L25 INTEREST ON PARTIAL COLLATTERAL LOAN C 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
1.111.25.74 BR# 74 L25 INTEREST ON PARTIAL COLLATTERAL C 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
LOAN

Notice on the 2nd record that the word "LOAN" is on a line by itself. It appears that this may be a word wrap from the program that created this source file (outside source). My code is attempting to skip/bypass these lines by checking for short lines (there is a \r\n immediately following the word LOAN in the 3rd record.

Here is a snippet of my code for viewing:

@GLC is a 4 element array
@GLA is a 12 element array

# Output Fixed Format GL Account Record
printf OFH
("%03d%03d%03d%03d%s%15s%-45s%-s%13d%13d%13d%13d%13d%13d%13d%13d%13d%13d%13d%13d%13d\n",@GLC,$GFT,$GLN,$GLD,$GLT,$GLO,@GLA);

Here is the output of a few records:

0011110250061 1.111.25.6BR# 6 L25 INTEREST ON PARTIAL COLLATTERAL LOAC 0 0 0 0 0 0 0 0 0 0 0 0 0
0011110250061 1.111.25.6BR# 6 L25 INTEREST ON PARTIAL COLLATTERAL LOANC 0 0 0 0 0 0 0 0 0 0 0 0 0

Notice that on the 1st recod the description is truncated to LOA as expected. However, on the 2nd record, the description is NOT truncated resulting in all characters to its right to be offset by 1 byte.

Please don't make fun of my rather short fieldnames s they are compliant with the client's data dictionary.

Any ideas GREATLY appreciated. I am sure that I am missing something here.

Thanks
 
BobMCT said:
... is truncated to LOA as expected.

How are you expecting this truncation to occur? Because of the field width specified in the format argument? If so, that's probably the problem... the field widths are minimums; if the supplied string is longer the field will be expanded, so you'll need to shorten the string yourself.

For example:
Code:
$ perl -e 'printf("%3s\n","hello world");'
hello world

Annihilannic.
 
Thank you Annihilannic;

I've been programming in perl for 10 years and I always thought the pattern provided was the maximum length, not the minimum length to accommodate. I've inserted a substr() to assure the maximum length of the source field and it is now working as expected.

Thank you again...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top