Midrange News for the IBM i Community


Posted by: Ringer
CLEAR RECFMT and VARLEN fields
has no ratings.
Published: 17 Jan 2013
Revised: 23 Jan 2013 - 4110 days ago
Last viewed on: 23 Apr 2024 (5093 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

CLEAR RECFMT and VARLEN fields Published by: Ringer on 17 Jan 2013 view comments(6)

I was wondering clearing a record format in RPG would set the VARYING character field lengths to zeros. Yep, it sure did, which is nice. FYI for anyone else. I don't see this behavior documented in the RPG reference PDF.

Chris Ringer

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: CLEAR RECFMT and VARLEN fields
Posted: 11 years 3 months 7 days 20 hours 38 minutes ago

Of course it would. by definition an "empty" VARYING field is '' (zero length).

So CLEAR myVARFld would set its length to 0 and may even clear out the existing data.

FYI, I normally do this:

 myVarField = '';

Because that's often more clear to others.

Posted by: Ringer
Premium member *
Comment on: CLEAR RECFMT and VARLEN fields
Posted: 11 years 3 months 7 days 20 hours 14 minutes ago

I presumed it would but had to check anyway. You know, Murphy's Law.

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: CLEAR RECFMT and VARLEN fields
Posted: 11 years 3 months 7 days 19 hours 32 minutes ago

Note the following regarding VARLEN/VARYING fields:

Increasing the length causes RPG to clear out the new locations before moving in the new data. So if a 50-byte VARYING field is currently 10 bytes long, and you move a 20 position value into it, positions 11 through 20 are cleared before the move is performed.

If you then move an 8 position value into that same field (which now has a "current length" of 20) the compiler simply moves the data into the field, left-justified and changes the current length to 8. This leaves the old data in position 9 to 20 and is what causes VARYING to perform better than fixed-length fields.

 

Posted by: Ringer
Premium member *
Comment on: CLEAR RECFMT and VARLEN fields
Posted: 11 years 3 months 7 days 2 hours 10 minutes ago

Thanks for the confirmation Bob.

Posted by: DaleB
Premium member *
Reading, PA
Comment on: CLEAR RECFMT and VARLEN fields
Posted: 11 years 3 months 7 days 19 minutes ago

Never thought about the performance aspect of VARYING vs. fixed; will have to keep that in mind. For the record, I've always explicitly zeroed the length:

 %LEN(myVarField) = 0;

Sounds like I should switch to CLEAR, which does exactly the same thing with less typing. Wink

Posted by: DaleB
Premium member *
Reading, PA
Comment on: CLEAR RECFMT and VARLEN fields
Posted: 11 years 3 months 2 days 4 hours 9 minutes ago

"I don't see this behavior documented in the RPG reference PDF."

Actually, it is documented. In Ch. 22 (Op Codes), CLEAR says it sets elements in a structure or a variable to their default initialization value depending on field type. In Ch. 9 (Data Types and Data Formats), Character Data Type, Variable-Length Character..., it says, "A variable-length field is initialized by default to have a current length of zero." QED