how to use datastructure in rpgle?
Announcement
Collapse
No announcement yet.
rpgle
Collapse
X
-
Re: rpgle
The RPG/400 program allows you to define an area in storage and the layout
of the fields, called subfields, within the area. This area in storage is
called a data structure. You can use a data structure to:
° Define the same internal area multiple times using different data
formats
° Operate on a field and change its contents
° Divide a field into subfields without using the MOVE or MOVEL
operation codes
° Define a data structure and its subfields in the same way a record is
defined
° Define multiple occurrences of a set of data
° Group non-contiguous data into contiguous internal storage locations.
In addition, there are three special data structures, each with a specific
purpose:
° A data area data structure (identified by a U in position 18 of the
data structure statement)
° A file information data structure (referred to by the keyword INFDS on
a file description specifications continuation line)
° A program-status data structure (identified by an S in position 18 of
the data structure statement).
Data structures can be program-described or externally described.
A program-described data structure is identified by a blank in position 17
of the data structure statement. The subfield specifications for a
program-described data structure must immediately follow the data
structure statement.
An externally described data structure, identified by an E in position 17
of the data structure statement, has subfield descriptions contained in an
externally described file with one record format. At compile time, the
RPG/400 program uses the external name to locate and extract the external
description of the data structure subfields. An external subfield name
can be renamed in the program, and additional subfields can be added to an
externally described data structure in the program.
Code:*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* IFilenameSqNORiPos1NCCPos2NCCPos3NCC................................* IFILEIN NS 01 1 CA 2 CB I....................................PFromTo++DField+L1M1FrPlMnZr...* I 3 18 PARTNO I 19 29 NAME I 30 40 PATNO I 41 61 DR IDsname....NODsExt-file++.............OccrLen+......................* IPARTNO DS I..............Ext-field+............PFromTo++DField+...............* I 1 4 MFG I 5 10 DRUG I 11 13 STRNTH I 14 160COUNT I* Figure 111. Using a Data Structure to Define Subfields within a Field The data structure subfields can be referred to by the PARTNO name or by the subfields MFG, DRUG, STRNTH, or COUNT. *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* IFilenameSqNORiPos1NCCPos2NCCPos3NCC................................* ITRANSACTNS 01 1 C1 2 C2 I....................................PFromTo++DField+L1M1FrPlMnZr...* I 3 10 PARTNO I 11 160QTY I 17 20 TYPE I 21 21 CODE I 22 25 LOCATN IDsname....NODsExt-file++.............OccrLen+......................* IPRTKEY DS I..............Ext-field+............PFromTo++DField+...............* I 1 4 LOCATN I 5 12 PARTNO I 13 16 TYPE I* Figure 112. Using a Data Structure to Group Fields When you use a data structure to group fields, fields from non-adjacent locations on the input record can be made to occupy adjacent internal locations. The area can then be referred to by the data structure name or individual subfield name. *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* I* I* A multiple-occurrence data structure is used to accumulate a I* series of totals for specific codes, and the totals of each of I* the occurrences of the data structure are written. I* The program-described data structure, TOTDS, has 99 occurrences I* (positions 46 and 47). The length of the data structure can be I* specified in positions 48 through 51. I* IDsname....NODsExt-file++.............OccrLen+......................* ITOTDS DS 99 I..............Ext-field+............PFromTo++DField+...............* I 1 50TOTCNT I 6 120TOT1 I 13 202TOT2 I* *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* C* C* A numeric code field, CODE, contains a value of 01 though 99. C* This value is different each time the OCUR operation is processed. C* When the OCUR operation is processed, the CODE field is used to C* set the current occurrence of TOTDS. If the OCUR operation is C* successful, the program branches to the ADDRTN subroutine where C* a record count is made and input values are added to the data C* structure subfields. If the CODE field contains a value other C* than 01 through 99, indicator 25 is set on and the program C* branches to BADCOD. C* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C CODE OCUR TOTDS 25 C 25 GOTO BADCOD C EXSR ADDRTN ________________ C " | Calculations | C " |________________| C BADCOD TAG C " ________________ C " | Calculations | C |________________| *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* C* C* When the totals for the specific codes in the multiple-occurrence C* data structure are to be written out, exception output is used. C* The EXCPT PRTHDG operation causes all exception lines in the C* output specifications with the name PRTHDG to be written. The C* do group initially sets field X to 1. The value in X sets the C* current occurrence of TOTDS. The Z-ADD operation adds TOTCNT to C* a field of zeros and places the sum in the result field TOTCNT. C* If TOTCNT contains a plus value, indicator 27 is set on. C* The EXCPT PRTDS operation causes the current occurrence of the C* data structure to be written. If overflow occurs while the C* current occurrence of the data structure is being written, the C* OF indicator is set on, a page skip occurs, and all exception C* lines in the output specifications with the name PRTHDG are C* written. The SETOF operation sets off the OF indicator. C* C* The Do group continues processing until field X is greater than C* 99, the maximum number of occurrences for the multiple-occurrence C* data structure. When X is greater then 99, control passes to the C* next statement following the END statement. C* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C* C EXCPTPRTHDG C DO 99 X 30 C X OCUR TOTDS C Z-ADDTOTCNT TOTCNT 27 C 27 EXCPTPRTDS C OF EXCPTPRTHDG C OF SETOF OF C END C " ________________ C " | Calculations | C |________________| *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* C* C* The ADDRTN subroutine updates the current occurrence of the C* multiple-occurrence data structure subfields. C* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C ADDRTN BEGSR C ADD 1 TOTCNT C ADD FLD1 TOT1 C ADD FLD2 TOT2 C* *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* OName++++DFBASbSaN01N02N03Excnam....................................* OPRINT E 206 PRTHDG O................N01N02N03Field+YBEnd+PConstant/editword+++++++++...* O " ________________________ O " |Entries for Report Title| O " |________________________| O " O E 2 PRTHDG O " __________________________________ O " |Entries for Report Column Headings| O " |__________________________________| O " O E PRTDS O X 10 O TOTCNTZ 20 O TOT1 J 35 O TOT2 J 50 O* Figure 113. Using a Multiple Occurrence Data Structure to Accumulate Totals-Example 1 In the following example, a multiple-occurrence data structure, TOTDS, is again used to accumulate a series of totals for specific codes and the totals of each of the occurrences of the data structure are written. There are 70 codes. *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* E* E* A compile-time array, ARC, is specified in the extension E* specifications. It has 70 entries. There are 10 entries in E* each record, and each array element is 6 positions long. The E* relative location of the alphanumeric code in the array (for E* example the 37th entry) sets the current occurrence of the data E* structure. E* E....FromfileTofile++Name++N/rN/tbLenPDSArrnamLenPDSComments++++++++* E ARC 10 70 6 ARRAY OF CODES *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* C* C* The Z-ADD operation sets field X to one. The LOKUP operation C* starts at the first element of ARC and searches until it finds C* the first element equal to the code in ACODE. The ACODE field C* is a character field of 6 characters. The index value, X, is C* set to the position number of the element located. If the LOKUP C* does not find an element equal to ACODE, indicator 20 is not set C* on and the GOTO operation conditioned by N20 branches to the C* BADCOD TAG. If LOKUP does find an element equal to ACODE, the C* OCUR operation uses the value in X to set the current occurrence C* of TOTDS and the program branches to the ADDRTN subroutine, where C* a record count is made and input values are added to the data C* structure subfields. The ADDRTN subroutine is not shown. If the C* occurrence is outside the valid range for the data structure, C* indicator 26 is set on, and the program branches to the ENDPRT TAG. C* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C Z-ADD1 X 30 C ACODE LOKUPARC,X 20 C N20 GOTO BADCOD C X OCUR TOTDS 26 C 26 GOTO ENDPRT C EXSR ADDRTN C " ________________ C " | Calculations | C BADCOD TAG |________________| C " C " C ENDPRT TAG ________________ C " | Calculations | C " |________________| *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* O* O* The calculations to print the data structure are not shown. O* Only part of the output specifications is shown. The PRTDS O* statement uses the value of field X, which contains the current O* occurrence of the data structure, as an index to print the O* corresponding alphanumeric code. O* OName++++DFBASbSaN01N02N03Excnam....................................* O E PRTDS O................N01N02N03Field+YBEnd+PConstant/editword+++++++++...* O ARC,X 10 O TOTCNTZ 20 O TOT1 J 35 O TOT2 J 50 Figure 114. Using a Multiple Occurrence Data Structure to Accumulate Totals-Example 2 Both programs (1 and 2) shown in Figure 115 use data area data structures (defined by the U in position 18 of the input specifications). Program 1 uses the subfields of the data structure to accumulate a series of totals. Program 2 then uses the totals in the subfields to do calculations. *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* I* PROGRAM 1 I* IDsname....NODsExt-file++.............OccrLen+......................* ITOTALS UDS I..............Ext-field+............PFromTo++DField+...............* I 1 82TOTAMT I 9 182TOTGRS I 19 282TOTNET I* *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C " ______________ C " | Calculations | C " |______________| C ADD AMOUNT TOTAMT C ADD GROSS TOTGRS C ADD NET TOTNET C* *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* I* PROGRAM 2 I* IDsname....NODsExt-file++.............OccrLen+......................* ITOTALS UDS I..............Ext-field+............PFromTo++DField+...............* I 1 82TOTAMT I 9 182TOTGRS I 19 282TOTNET I* *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C " ______________ C " | Calculations | C " |______________| C AMOUNT2 COMP TOTAMT 9191 C GROSS2 COMP TOTGRS 9292 C NET2 COMP TOTNET 9393 C " C " C* Figure 115. Data Area Data Structures *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* IDsname....NODsExt-file++.............OccrLen+......................* IDSONE E DSEXTREC I..............Ext-field+............PFromTo++DField+...............* I CHARACTER CHAR I 1 16 CHZON *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++++* A R RECORD TEXT('EXTERNALLY DESCRIBED RECORD') A CHARACTER 10 A ZONED 6S 2 A PACKED 4P 0 A BINARY 4B 0 A* Figure 116. Renaming Subfields in an Externally Described Data Structure On the data structure statement shown in Figure 116, positions 7 through 12 contain the name of the data structure being defined (DSONE), position 17 contains an E to denote externally described, and positions 19 and 20 contain DS to denote data structure. Positions 21 through 30 contain the name of the file (EXTREC) whose first record format contains the field descriptions used as the subfield descriptions for this data structure (RECORD). On the first data description specification, position 17 contains an R to denote record format and positions 19 through 28 contain the name of the record format (RECORD). On subsequent data description specifications, positions 19 through 28 contain the names of the fields (CHARACTER, ZONED, PACKED, and BINARY). Fields in a data structure can also be redefined for program use. Fields CHARACTER and ZONED are also described as one field (CHZON) in the input specifications. In the RPG/400 program, a field name can contain no more than 6 characters. Therefore, the field name CHARACTER is renamed CHAR in the input specifications. The data structure then uses CHAR as the subfield name.
A data area data structure, identified by a U in position 18 of the data
structure statement, indicates to the RPG/400 program that it should read
in and lock the data area of the same name at program initialization and
should write out and unlock the same data area at the end of the program.
Data area data structures, as in all other data structures, have the type
character. A data area read into a data area data structure must also be
character. The data area and data area data structure must have the same
name unless you rename the data area within the RPG/400 program by using
the *NAMVAR DEFN statement.
You can specify the data area operations (IN, OUT, and UNLCK) and have the
type for a data area that is implicitly read in and written out. Before
you use a data area data structure with these operations, you must specify
that data area in the result field of the *NAMVAR DEFN statement.
A data area data structure cannot be specified in the result field of a
PARM operation.
If you specify blanks for the data area data structure (positions 7
through 12 of the input specifications line that contains a U in position
18), the RPG/400 program uses a local data area. To provide a name for a
local data area, use the *NAMVAR DEFN operation, with *LDA in factor 2 and
the name in the result field.
Code:*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* I* I* In this example, the data area data structure, TOTALS, is I* implicitly read and written by RPG and explicitly updated. At I* program initialization, RPG implicitly retrieves and locks TOTALS I* After calculations are performed on TOTALS, the *LOCK OUT state- I* ment updates and maintains the lock status of TOTALS. At end of I* program, RPG implicitly writes and unlocks TOTALS. I* TOTAL PROGRAM I* IDsname....NODsExt-file++.............OccrLen+......................* ITOTALS UDS I..............Ext-field+............PFromTo++DField+...............* I 1 82TOTAMT I 9 182TOTGRS I 19 282TOTNET *...1....+....2....+....3....+....4....+....5....+....6....+....7...* CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++* C " ______________ C " | Calculations | C " |______________| C ADD AMOUNT TOTAMT C ADD GROSS TOTGRS C ADD NET TOTNET C *LOCK OUT TOTALS C " ______________ C " | Calculations | C " |______________| C ADD AMOUNT TOTAMT C ADD GROSS TOTGRS C ADD NET TOTNET C " ______________ C " | Calculations | C " |______________| C *NAMVAR DEFN TOTALS
If the RPG/400 program is a pre-started job that is to receive program
initialization parameter (PIP) data, the PIP Data Area (PDA) can be used
to retrieve the data. To define the PDA, use the *NAMVAR DEFN operation
code, and after acquiring the requesting program device, issue an IN
operation code with factor 2 specifying the name of the PDA you defined in
the DEFN operation code.
Unlike other data areas, you cannot LOCK, UNLOCK, or write data to a PDA
using the OUT operation code. For more information on how to define PDAs
see the RPG/400 Reference.
Code:D* D* The file BUDGET hold budget info both $dollars and expected sold qtys D* We just want the money, so it starts in position 6 and ends with 77. D* Its also stored by month (thats why 12) so now we have all $'s for all D* months in one place BUD D* D E DS EXTNAME(BUDGET) D BUD 6 77P 2 D DIM(12)
All my answers were extracted from the "Big Dummy's Guide to the As400"
and I take no responsibility for any of them.
www.code400.com
-
Re: rpgle
Since your original question specifically requested information on data structures in "rpgle", I've taken the liberty of converting Jamie's examples from RPG III to RPG IV.
Code:*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... D PARTNO DS D*ame+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D MFG 1 4 D DRUG 5 10 D STRNTH 11 13 D COUNT 14 16 0 - OR - D PARTNO DS D*ame+++++++++++ETDs.......Length+IDc.Keywords+++++++++++++++++++++++++++++ D MFG 4 D DRUG 6 D STRNTH 3 D COUNT 3 0 Figure 111. Using a Data Structure to Define Subfields within a Field *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* .....D*ame+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D PRTKEY DS D LOCATN 1 4 D PARTNO 5 12 D TYPE 13 16 - OR - D PRTKEY DS D LOCATN 4 D PARTNO 8 D TYPE 4 Figure 112. Using a Data Structure to Group Fields *.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..* D* D* A multiple-occurrence data structure is used to accumulate a D* series of totals for specific codes, and the totals of each of D* the occurrences of the data structure are written. D* The program-described data structure, TOTDS, has 99 occurrences D* (positions 46 and 47). The length of the data structure can be D* specified in positions 48 through 51. D* .....D*ame+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D TOTDS DS OCCURS(99) D TOTCNT 1 5 0 D TOT1 6 12 0 D TOT2 13 20 2 - OR - D TOTDS DS OCCURS(99) D TOTCNT 5 0 D TOT1 7 0 D TOT2 8 2
"Time passes, but sometimes it beats the <crap> out of you as it goes."
Comment
-
Re: rpgle
Michael,
You are right he did say RPGLE ....
(I adjusted your title and rating-I know people)
I was doing the husband thing and just reading the first couple of words then just filled in the blanks
Anyway Here is a program....
Code:*================================================================= * data area somespace is created in QTEMP for the example * This program takes in a 256 char string of data from a dataarea * then it breaks the fields up using a data-structure and then * updates info in the dataarea usind the data-structure and the * OUT opcode. *================================================================== * d SDS d @PGM 001 010 d @PARMS 037 039 0 d @JOB 244 253 d @USER 254 263 d @JOB# 264 269 0 * * Variable Definition * d AreaName s 10 inz('SomeSpace') d CmdString s 256 d CmdLength s 15 5 d Q s 1 inz('''') d TodayIso s D * * you may also use a physical file to define the * fields rather than defining them in * the program. * d**>somespace e ds extname(DTAAPF) * * else it would look like this * d someSpaceDS ds 256 dtaara(SomeSpace)inz d PgmUser 10 d PgmDate 8 0 d PgmTime 6 0 d PgmSeq 7 0 d Therest 225 * * ================================================ * M A I N L I N E * ================================================ * * Create the data area in qtemp * CRTDTAARA DTAARA(QTEMP/DATAAREA) TYPE(*CHAR) LEN(256) TEXT('test data area') * c eval CmdString = 'CRTDTAARA DTAARA(QTEMP/'+ c %trim(AreaName) + ' ) TYPE(*CHAR) LEN(256)' + c ' TEXT(' + Q + 'test data area' + Q + ')' c eval CmdLength = %len(%trim(CmdString)) c call(e) 'QCMDEXC' c parm CmdString c parm CmdLength * * get the info from the data area - we lock it cause we are going to * update * c *lock in somespaceDs * * populate the data structure * c if PgmUser = *blanks c clear PgmSeq c endif * c eval PgmUser = @User c eval TodayIso = %date() c clear PgmDate c eval PgmDate = c %uns(%char(%Date():*Iso0)) * c clear PgmTime c time PgmTime * * Increment the counter * c eval PgmSeq += 1 * c out somespaceDs * c eval *INLR = *on
Attached Files
Comment
Comment