rpg iv advanced data definition and manipulation- chapter 11
TRANSCRIPT
RPG IV
Advanced Data Definition and Manipulation- Chapter 11
Objectives
Perform date arithmeticDefine data that facilitate data
manipulation, error trapping, debugging and program maintenance
Use RPG IV built-in functions(BIF’s) to manipulate data
Data Types and Time
Three data types exist for dealing with time: Date(D) -10 byte display yyyy-mm-dd Time(T)- 8 byte display hh.mm.ss Timestamp(Z) - 26 byte default display yyyy-
mm-dd-hh.mm.ss.mmmmmm
Date supports 8 different formatsTime supports 5 different formatsDifferent separators are supported
Date and Time Operations
ADDDUR, SUBDUR and EXTRCTDuration codes:
*YEARS (or *Y) *MONTHS (or *M) *DAYS (or *D) *HOURS (or*H) *MINUTES (or *MN) *SECONDS (or *S) *MSECONDS (or *MS)
ADDDUR(Add Duration)
Add a duration coded in Factor 2 to the date specified in Factor 1
Storing the answer in the date, time or timestamp field specified as the Result field
Factor 2 must contain an integer field or literal
SUBDUR(Subtract Duration)
Has two uses: Subtract a date/time duration from a
date/time value (similar to ADDDUR) Calculate the duration between two
date/time values
Subtract a Date/Time Duration
Subtract a duration coded in Factor 2 to the date specified in Factor 1
Storing the answer in the date, time or timestamp field specified as the Result field
Factor 2 must contain an integer field or literal
Calculate a Duration Between Two Date/Time Values
Factor 1 and Factor 2 must contain data items of compatible types
Place an integer receiveing field followed by a duration code in the Result field
EXTRCT(Extract Date/Time/Timestamp)
Extract a portion of a data/time or timestamp and store in the Result field
Factor 2 date/time data item must be coupled with a duration code to signal which portion of the data/time unit is to be extracted
Pointer Data Types
Two kinds of pointer data types: basing pointers and procedure pointers
Used to access storage that is allocated dynamically throughout the program
Pointers store address of memory locations rather than data values
Procedure pointers must include the keyword PROCPTR
LIKE (Field Definition)
The keyword LIKE allows you to define a field based on the attributes of another field
The entered number signals the extent of the increase or decrease in field length
Data Structures
Subdivide fields into subfieldsRestructure records with different
field layoutsChange field data typesAdd a second dimension to arrays
Data Structures cont.
DS coded in 24-25 on D spec signals the beginning of a data structure
Enter name of data structure in 7-21Follows same rules as fieldUse absolute notation or length
notation for subfields
OVERLAY
OVERLAY keyword indicates that a subfield overlays the storage of another subfield
If the data time within parentheses is an array, OVERLAY applies to each element of the array
Externally Described Data Structures
Externally described data structures are useful when you want to standardize a data structure
Code an E in position 22Name the external file using the
EXTNAME keyword
Multiple-Occurrence Data Structures
Repeated data structuresKeyword OCCURSNumber determines how many times
the structure is repeated in storage
OCCUR (Set/Get Occurrence of a Data Structure)
OCCUR operation is used to establish which occurrence of the data structure is used next within the program
Factor 1 specifies which occurrence should be made current
Factor 2 contains the name of the DS
Initialization and Reinitialization of VariablesINZ keyword will initialize to default
value of data type*INZSR is a subroutine that will be
invoked automatically after the program completes its other start up tasks
CLEAR and RESET will allow you to reinitialize variables
File-Information Data Structures
A special data structure that can be defined for each file used by your program
This data structure contains subfields that provide info to your program about the file and I/O operations
The required entries in the F spec are; keyword INFDS and a parameter with the name of the data structure defined in the D Spec
Program-Status Data Structures
A data structure that can provide you with information about the program itself and about exceptions/errors that occur during program execution
An S in position 23 of a data structure definition line identifies a program-status data structure
Error Handling
Without explicit error handling within your program, any runtime error will cause the system to suspend the program
One technique to code the (E) error-handling extender with those operations that permit such an entry
Then perform an error routine if the %ERROR bif is on
*PSSR
Another error handling technique is to use a special subroutine called *PSSR
This subroutine will automatically receive control when a program error occurs
To send control to this subroutine for file errors, you must explicitly designate *PSSR as the error handler for the file
Field Inspection
%SIZE(Get Size in Bytes)%LEN(Get or Set Length)%DECPOS(Get Number of Dec Positions)%ELEM(Get Number of Elements)TESTN(Test Numeric)TEST(Test Date/Time/Timestamp)SCAN(Scan String)%SCAN(Scan for Characters)CHECK(Check Characters)CHECKR(Check Reverse)
%SIZE
Returns number of bytes or length of stringUsed to determine size of field, literal,
named constant, data structure, subfield, array or table
One required parameter to represnet data whose size you want to know
Second parameter *ALL- size of entire table, array or data structure
%LEN
Returns number of digits or characters in an expression
Especially useful with character expressions
For numeric expressions %LEN returns the precision of the expression
%DECPOS
Returns the number of decimal positions in a numeric variable or expression
Sometimes used with the %LEN function
%ELEM
Returns the number of elements in an array or a table or the number of occurrences in a multiple-occurrence data structure
Can be used in calculations or when defining other fields
TESTN
Determine whether a character field contains all numeric characters, leading blanks followed by all numeric characters, or all blanks
Used to validate fields before performing calculations
TEST
Checks validity of date, time or timestamp fields
Checks character and numeric fields for valid date/time data
SCAN
Look for a characer or string of character within a character field
Case sensitive - recognizes upper and lower case
Blanks are not ignored
%SCAN
Nearly identical to SCANPerforms the scan within an
expression
CHECK
Similar to SCANChecks for discrepancies between
individual characters of compare field and base string and signals absence of a base character from set of compare characters and stores it’s location in the result field
Checks left to right
CHECKR
Works exactly like CHECKChecks the base string from right to
left
Field Character Manipulation
+ (Concatenate Character Strings)%TRIM, %TRIML, and %TRIMR (Trim
Blanks)%SUBST(Get Substring)%DEC(Convert to Packed Decimal
Format)XLATE (Translate)
Concatenate Character Strings
Using the + operator with EVAL is a convenient way to concatenate two (or more) strings to form a new string
Trim Blanks
Remove leading and/or trailing blanks from their argument
%TRIML – removes leading blanks%TRIMR – removes trailing blanks%TRIM – removes both leading and
trailing blanks
%SUBST
Extracts a portion of a character stringArgument parameters in order: string
from which extraction is to occur, position within that string where substring is to start and optional length of substring, parameters separated by a colon
%DEC
Converts the result of the expression to a packed-decimal format
%DECH (Convert to Packed Decimal Format with Half Adjust)
%INT (Convert to Integer Format)%UNS(Convert to Unsigned Format)%INTH and %UNSH are half adjust built
in functions
XLATE
Translate or convert characters within a string to other characters
Points to Remember
Fields can be defined relative to other field definitions using the keyword LIKE
You can define data structures that represent complex definitions of a given area of memory
Points to Remember cont.
Data structures or subfields of data structures can be initialized easily as part of the data structure definition
Two special data structures of predefined subfields exist in RPG to provide information about the status of files used by the program or about the processing of the program itself