CLEAR RECFMT and VARLEN fieldsPublished 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.
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.
I presumed it would but had to check anyway. You know, Murphy's Law.
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.
Thanks for the confirmation Bob.
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.
"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