Advanced RPG - FVTC IT | Home
Download
Report
Transcript Advanced RPG - FVTC IT | Home
Advanced RPG
Chapter 11
Advanced Data Definition and
Manipulation
Data Types
• (From Chapter 3)
– Character
– Numeric
– Date
– Boolean
– DBCS
– Pointer
Data Types
• Date Types and Time defaults
– Date: yyyy-mm-dd (10 byte)
– Time: hh.mm.ss (8 byte)
– Timestamp: yyyy-mm-dd-hh.mm.ss.mmmmmm (26
bytes)
• Alternate displays are available:
– *MDY, *DMY, *EUR
• Set by using either DATFMT or TIMFMT keyword\
• Examples: p. 232
Data Types
• All date and time formats allow you to refer
to them by:
– *YEARS (or *Y)
– *MONTHS (*M)
– *DAYS (*D)
– *HOURS (*H)
– *MINUTES (*MN)
– *SECONDS (*S)
– *MSECONDS (*MS)
Data Types
• Op Codes:
– ADDDUR: Allows you to add duration coded
in Factor2 to date in Factor1, with answer
going to result field. (If value is negative the
duration is subtracted not added)
• Example on page 233
– SUBDUR: Two uses, One to subtract a
date/time duration (similar to ADDDUR),
Second is to calculate the duration between
two date/time units.
Data Type
• Op Codes:
– EXTRCT: Extracts a portion of the Date, Time, or
Timestamp and stores it in the result field. Factor 1 is
always blank.
• Cannot use EVAL op code with the – or + with
date/time fields. Must use the Add/Sub Dur op
code.
• Also cannot determine day of week unless you
compare it to a known Sunday. ie 12/31/1899
Data Type
• Pointer Data Types: store addresses of
memory locations instead of the actual
data located there.
– Tells you where to find something not what is
stored there.
– Example P.236
• Used in the C programming language but
not widely used in RPG!!
Data Type
• Like (field definition)
– Defining a field length or type by referencing a
second field that provides the definition.
D NewField
S
LIKE(OldField)
• OldField can be another D spec or a field from a
file.
– Can place + or – in the length to either add or
subtract from the referenced size. P. 237
Data Type
• Field Inspection:
– built-in functions and operations that let you
inspect the size and properties of data items
or test individual positions or bytes within a
character field.
• %SIZE: returns number of bytes or length of
argument. (returns size not digits!!)
• %LEN: returns the number of digits or characters
in an expression. (char-significant digits, numericprecision of expression)
Data Type
• Field Inspection:
– %DECPOS: returns the number of decimal
positions of a numeric variable
– %ELEM: returns the number of elements in
an array or table or number of occurrences in
a multiple occurrence data structure.
– TESTN: test to see if character field contains
all numeric characters, leading blanks or all
blanks
• Useful in validating fields for numeric use.
Data Type
• Field Inspection:
– TEST: checks the validity of date, time or
timestamp fields.
• Must specify specific type otherwise default is
used.
– SCAN: lets you look for a character or string
of characters within a character field.
• Direction is left to right and is case sensitive
• %Found is turned on if successful
• Result field can contain position it found string or 0
if it does not find string
Data Type
• Field Inspection:
– %SCAN: nearly identical to SCAN operation,
but lets you perfrom the scan within an
expression.
• Cannot search an array or use %FOUND bif
– CHECK: Similar to SCAN. CHECK verifies
each character in Factor 2 is among the valid
characters in Factor 1; if mismatch it stores
the location of the mismatch in result field.
• Useful for verification of a string
Data Type
• Field Inspection:
– CHECKR: Works exactly like CHECK but in reverse
order, right to left.
• Field Manipulation:
– +: Concatenation Character String
• If result field is to large it pads with blanks
• If result field is to small it truncates on the right
– %TRIM: Trim blanks at edges
– %TRIML: Trim leading blanks
– %TRIMR: Trim trailing blanks
Data Type
• Field Manipulation:
– %SUBST: Extracts a character string
• Can specify starting position and length
– %DEC: Converts the result of an expression
to a packed decimal format.
– XLATE: lets you translate characers within a
string to other characters.
• From and To strings must be same length
Data Area
• Data Area Data Structure
– Defined by a U in position 23
– If no name provided it is represented by LDA
– Works similar to a file
• Loaded when program is run
• Written back to data area when program ends
Data Areas
• Data Areas: as/400 objects that represent
storage locations used to share data
between programs within a job or between
jobs.
• Local Data Area: created for each job
– 1024 positions long, type character, initially
filed with blanks.
– when job ends lda is gone
– p.223
Data Areas
• Can access Data Areas without defining
them in D specs (reading into another one
you do have specified).
– Use *DATAARA in Factor1
– Result field contains variable to receive
contents
– p. 224
– Not automatically retrieved or written back to
external data area, you must program this.
Data Areas
• *DATAARA
– IN: Retrieves a data area
– OUT: Writes back to data area
– UNLOCK: Unlocks data area
Data Type
• Program Described Data Structures: allow
you to subdivide fields into smaller fields.
– P. 238
– Overlay keyword: subfield overlays storage of
another subfield
• Different way of subdividing the field.
– Used to group fields from file that are not
adjacent. P. 239
Data Type
• Externally Described Data Structures
– Format defined in DDS and refer to it in D
specs.
– p. 240
• Multiple Occurrence Data Structures
– Structure is repeated in storage by OCCURS
(##) keyword.
– Similar to an array or table but much more
complex collection of elements.
Data Type
• Initializing Data Structures
– Will contain blanks at start of program unless
you specify initialized value. (even numeric
data types!!)
– Keyword INZ(value)
– Or use built in *INZSR initialize subroutine.
Data Type
• File Information Data Structure:
– special data structure defined for each file
used in your program.
• Contains predefined fields that can provide
information to your program about the file and I/O
operations.
• Helpful in debugging
• Keyword INFDS and name of data structure on F
spec (p. 247)
Data Type
• File Information Data Structure:
– Usefulness:
• Position 1 – 8: File Name
• Position 16 – 21: contain last op code used on file
• Position 11 – 15: five-digit I/O Status code
–
–
–
–
–
00000: no error or exception
00002: Function key was used to end display
00011: signals end of file on read
00012: signals a no-record found condition on a chain
> 00999: error code
» 01218 indicates a locked record
» 01021 indicates attempt to write duplicate key record
Data Type
• Program status data structure: provide
information about the program itself and
about exceptions or errors that occur
– similar to file with predetermined subfields
that automatically acquire values during
execution.
• Name of Program
• Job name
• Who called it
Data Type
• Status Codes:
–
–
–
–
00000: no error occurred
00101: negative square root
00102: divide by zero
00907: decimal data error
– S in position 23 of DS definition indicates a
Program Status Data Structure.
• p. 249
Data Area
• Error Handling:
– Without explicit error handling within your program,
any runtime error will cause the system to suspend
the program and send an interactive message to the
user.
• (E) extender can be used p.249
– Causes program to ignore error and continue
• *PSSR: built in subroutine name that is called when an error
occurs p. 250
– Combines file and program handling into one area.
– You can control what happens when error is encountered.