Problem solve Get help with specific problems with your technologies, process and projects.

"CLEAR" advantage over resetting work fields

Use the CLEAR operation code to clear whole records in one statement.

John Kohan

At times we need to reset the value of work fields, data buffers and screen fields. If a field is an alphanumeric type, then we may move "*BLANKS" when we want it "reset". If a field is numeric, then we may "Zero Add" "*ZEROS" to the field.

But the "CLEAR" operation code provides much more flexibility. By using the "CLEAR" operation code, the fields reset to their default initialization value depending on field type (numeric, character, graphic, UCS-2, indicator pointer, or date/time/timestamp). This way you just "CLEAR" the field and let the computer reset it to the proper value.

In a single occurrence data structure, all fields are cleared in the order in which they are declared within the structure. For multiple occurrence data structures you can clear the "current" table element or if you specify "*ALL", then all elements are reset. The same holds true for arrays. If you clear ARRAY(1), then element one of the array is cleared. But if you clear ARRAY, then all the elements in the array are cleared.

You may also clear record formats on screens. If you are writing to a subfile with 10 workflields, you can clear the "SUBFILE" record format of the screen and all 10 records are reset. They can be a mix of alphanumeric and numeric, the CLEAR operation will deal with it.

You may also clear record formats of files. If you CLEAR the record format of a physical or logical file, then all the fields are reset to their default initialization value. You may also specify an optional value of *NOKEY and all the fields are cleared EXCEPT the key fields. This comes in handy when you read the last record in a file to get the last "key" used, add one to the value and clear with *NOKEY. You end up with a new initialized record with the key value already set.

So no matter what type of field, array, data structure or file, the CLEAR operation code will reset everything to the default value. The code becomes much easier to read, since you can clear whole records in one statement.

About the author: John Kohan is a senior programmer analyst at CT Codeworks.


  • Using date data-type fields in RPG IV
    Date data-types are incredibly powerful and flexible, but figuring out how to use them to do date format conversions can be frustrating. Get help in this tip from Search400 member Richard Jarboe.
  • Select only the data that you want
    Do you know that you can create a logical file that has the selection you need along with the order in which you want the records? In DDS, there is a way to create a logical view of a physical file that you may select or omit the data that meets a certain criteria. Search400 expert John Kohan provides an example of how you can do this.
  • A new and improved find command
    Search400 member Larry Pepin has developed over the years a scan command that scans source files. Not only does this command give what I believe is a better and more readable printout than the FNDSTRPDM command because it highlights the found scan strings, but it also has other very unique features.
  • Browse all programmer tips
  • Search400's Best Developer Web Links: tips, tutorials and more.
  • Ask your programmer questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our programmer gurus are waiting to answer your technical questions.

Dig Deeper on iSeries CL programming