rpg iv advanced data definition and manipulation- chapter 11

41
RPG IV Advanced Data Definition and Manipulation- Chapter 11

Upload: arabella-austin

Post on 26-Dec-2015

231 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: RPG IV Advanced Data Definition and Manipulation- Chapter 11

RPG IV

Advanced Data Definition and Manipulation- Chapter 11

Page 2: 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

Page 3: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 4: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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)

Page 5: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 6: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 7: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 8: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 9: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 10: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 11: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 12: RPG IV Advanced Data Definition and Manipulation- Chapter 11

Data Structures

Subdivide fields into subfieldsRestructure records with different

field layoutsChange field data typesAdd a second dimension to arrays

Page 13: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 14: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 15: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 16: RPG IV Advanced Data Definition and Manipulation- Chapter 11

Multiple-Occurrence Data Structures

Repeated data structuresKeyword OCCURSNumber determines how many times

the structure is repeated in storage

Page 17: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 18: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 19: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 20: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 21: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 22: RPG IV Advanced Data Definition and Manipulation- Chapter 11

*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

Page 23: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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)

Page 24: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%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

Page 25: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%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

Page 26: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%DECPOS

Returns the number of decimal positions in a numeric variable or expression

Sometimes used with the %LEN function

Page 27: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%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

Page 28: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 29: RPG IV Advanced Data Definition and Manipulation- Chapter 11

TEST

Checks validity of date, time or timestamp fields

Checks character and numeric fields for valid date/time data

Page 30: RPG IV Advanced Data Definition and Manipulation- Chapter 11

SCAN

Look for a characer or string of character within a character field

Case sensitive - recognizes upper and lower case

Blanks are not ignored

Page 31: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%SCAN

Nearly identical to SCANPerforms the scan within an

expression

Page 32: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 33: RPG IV Advanced Data Definition and Manipulation- Chapter 11

CHECKR

Works exactly like CHECKChecks the base string from right to

left

Page 34: RPG IV Advanced Data Definition and Manipulation- Chapter 11

Field Character Manipulation

+ (Concatenate Character Strings)%TRIM, %TRIML, and %TRIMR (Trim

Blanks)%SUBST(Get Substring)%DEC(Convert to Packed Decimal

Format)XLATE (Translate)

Page 35: RPG IV Advanced Data Definition and Manipulation- Chapter 11

Concatenate Character Strings

Using the + operator with EVAL is a convenient way to concatenate two (or more) strings to form a new string

Page 36: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 37: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%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

Page 38: RPG IV Advanced Data Definition and Manipulation- Chapter 11

%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

Page 39: RPG IV Advanced Data Definition and Manipulation- Chapter 11

XLATE

Translate or convert characters within a string to other characters

Page 40: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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

Page 41: RPG IV Advanced Data Definition and Manipulation- Chapter 11

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