RPG IV - Chapter 3
Download
Report
Transcript RPG IV - Chapter 3
Defining Data with Definition
Specifications
Chapter 3
Presentation © Copyright 2002, Bryan Meyers
Objectives
• Define work fields, data structures and
other data items
• Identify the appropriate data type for data
items
• Set the initial value for data items
• Distinguish among program variables,
literals and constants
Programming in RPG IV
Third Edition
2
Definition Specifications
• Define all fields in your program
• Definition specifications define work fields
(standalone variables)
– Input specifications define fields from database
– S in position 24 indicates standalone variable
• Specify name, length, and decimal positions (if
any)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D TotalDue
S
7 2
D CtyStZip
S
40
Programming in RPG IV
Third Edition
3
Numeric Literal
• May include a decimal point and/or sign
• Sign must be the left most character of
the literal
• May include digits 0 thru 9
• Should never contain commas, dollar
signs, or percent signs
• Must not be enclosed in apostrophes
Programming in RPG IV
Third Edition
4
Character Literals
• To indicate that a value is a character
literal simply enclose it with apostrophes
Programming in RPG IV
Third Edition
5
Figurative Constants
• *BLANK(S)
– Fills a character field with
blanks
• *HIVAL
– Fills a character with X’FFFF
(all bits on)
– Fills a numeric field with all 9s
and + sign
• *LOVAL
– Fills a character with x’000
(all bits off)
– Fills a numeric field with all 9s
and - sign
• *ALL
– Causes a string to be
repeated in a field
• *OFF
– Character value of ‘0’
• *ON
– Character value of ‘1’
• *NULL
– Used with pointers
• *ZERO(S)
– Fills a numeric field with
zeros
Programming in RPG IV
Third Edition
6
Assigning Initial Values to Data
• Specify the value using the INZ keyword
in the field definition
• The value is indicated using a literal or a
figurative constant
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D MaxLimit
S
7 2 INZ(10500.00)
D CompName
S
40
INZ(‘Kay Elmnop Enterprises’)
D HighLimit
S
7 2 INZ(*HIVAL)
Programming in RPG IV
Third Edition
7
Zoned vs. Packed Decimal
• Zoned decimal requires a full byte of storage
– Data type S in position 40
• Packed decimal use a compressed storage format
– Data type P in position 40
• Integers and binary numbers require even less memory
than the decimal data types do
– Data type I (integer)
– Data type U (unsigned integer)
– Data type B (binary)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D TotalDue
S
7S 2
D TotalDue2
S
7P 2
D UpDown
S
5I 0
D RecCount
S
10U 0
Programming in RPG IV
Third Edition
8
Date, Time, and Timestamp
• Timestamp is a combination of date and
time
• Define a standalone field, put a D (for
dates), T (for times) and Z (for timestamp)
in column 40
– Do not specify length or decimal positions
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D EnrollDate
S
D
D StartTime
S
T
D TransTime
S
Z
Programming in RPG IV
Third Edition
9
Indicator Data Type
• Referred to in other languages as
Boolean data type
• Code N in column 40
• Must be a single byte
• You can initialize to *ON or *OFF
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D InpError
S
N
INZ(*OFF)
Programming in RPG IV
Third Edition
10
Defining Constants
• A named constant differs from a standalone in
two respects
– Value never changes during processing
– Defined with no specified length
• Code the letter C, for constant, in column 24
• Enter the value in the Keywords area
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D FICA
C
.0765
D ExVicePres
C
'John Adams'
D PhoneEdtWd
C
'(
)
'
D IndDay
C
D'1776-07-04'
Programming in RPG IV
Third Edition
11
Defining Data Structures
• Subdivide fields into subfields
• Change field data types
• Data structure has two parts
– Data structure header
– Definition for the subfields
• DS coded in 24-25 on D spec signals the
beginning of a data structure
• Enter name of data structure in 7-21
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D OptName
DS
D SubfieldA
3 0
D SubfieldB
5 2
Programming in RPG IV
Third Edition
12
Defining Data Structure Subfields
• Length notation
– Leaves From blank and enters the subfield
length in the To positions
• Absolute notation
– From and To indicate the beginning and
ending positions of the subfield
• Length notation is preferred
Programming in RPG IV
Third Edition
13
Overlapping Subfields
• Locations of subfields can overlap
• Length notation uses the keyword OVERLAY
– Absolute notation indicates the overlap with the From and To
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
DName+++++++++++ETDsFrom+++To/Len+IDc.Keywords+++++++++++++++++++++++++++++
D
DS
D Phone
10 0
D AreaCode
3 0 OVERLAY(Phone)
D Exchange
3 0 OVERLAY(Phone:4)
D LocalNbr
4 0 OVERLAY(Phone:7)
//Using OVERLAY with *NEXT
D
DS
D Phone
10 0
D AreaCode
3 0 OVERLAY(Phone)
D Exchange
3 0 OVERLAY(Phone:*NEXT)
D LocalNbr
4 0 OVERLAY(Phone:*NEXT)
Programming in RPG IV
Third Edition
14
Points to Remember
• RPG IV requires you to define all fields
your programs will use
• Input fields (from files) are defined using
Input Specifications
• Standalone fields are defined using
Definition Specifications
• You can use the keyword INZ to assign a
value to a field
Programming in RPG IV
Third Edition
15
Points to Remember
• Figurative constants are built-in literals
with specified values
• Definition Specifications are used to
define named constants as well as
standalone fields
• RPG IV supports many data types
– Character, numeric, date, Boolean, etc.
• Data structures let you subdivide fields
into subfields and redefine fields
Programming in RPG IV
Third Edition
16