Escolar Documentos
Profissional Documentos
Cultura Documentos
Chapter 12
Objectives
Understand functions to inspect or manipulate data items Work with character strings Discuss error handling techniques
Field Inspection
%SIZE (Get Size in Bytes) %LEN (Get or Set Length) %DECPOS (Get Number of Dec Positions) %ELEM (Get Number of Elements) TEST (Test Date/Time/Timestamp) %SCAN (Scan String) %CHECK (Check Characters)
%CHECKR (Check Reverse)
Programming in RPG IV Third Edition 3
%SIZE
Returns number of bytes for field, literal, named constant, data structure, subfield, array or table One required parameter to represent data whose size you want to know
Optional *ALL second parameter to get size of entire table, array or data structure
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE SIZ = %SIZE(FieldA); SIZ = %SIZE(ArrayX); SIZ = %SIZE(TableX:*ALL); SIZ = %SIZE(gurubesar); /END-FREE
Programming in RPG IV Third Edition 4
%LEN
Returns number of digits or characters in an expression
For numeric expressions %LEN returns the precision
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Salary S 9P 2 INZ(48600) D Tenure S 5P 0 INZ(1) D Name S 15 INZ(John Doe) D Name2 S 15 VARYING INZ(John Doe ) /FREE Length1 = %LEN(Salary); // Length1 = 9 Length2 = %LEN(Salary * Tenure); // Length2 = 14 Length3 = %LEN(Name); // Length3 = 15 Length4 = %LEN(%TRIM(Name)); // Length4 = 8 Length5 = %LEN(Name2); // Length5 = 8 /END-FREE
Programming in RPG IV Third Edition 5
%DECPOS
Returns the number of decimal positions in a numeric variable or expression Sometimes used with the %LEN function
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Salary S 9P 2 INZ(48600) /FREE Length = %LEN(Salary); // Length = 9 Decimals = %DECPOS(Salary); // Decimals = 2 /END-FREE
Programming in RPG IV Third Edition 6
%ELEM
Returns the number of elements (occurrences) in an array, table or multiple occurrence data structure Can be used in calculations or when defining other fields
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Index S 5U 0 D Location S 4 0 DIM(100) D LocatTotal S 10 2 DIM(%ELEM(Location)) /FREE FOR Index = 1 to %ELEM(LocatTotal); ENDDO; /END-FREE
Programming in RPG IV Third Edition 7
TEST
Checks validity of date, time or timestamp fields
%ERROR returns *ON for invalid value
%SCAN
Looks for a character or string in a character field
Returns an integer indicating position of character
%CHECK
Returns location of mismatch between listed characters and compare field
Signals absence of a base character from set of compare characters
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE Greeting = Hello + World; // Greeting now contains Hello World FullName = FirstName + + MidInitial + . + LastName; /END-FREE
Programming in RPG IV Third Edition 12
%TRIM Functions
Remove leading and/or trailing blanks from their argument
%TRIM removes leading and trailing blanks %TRIML removes leading blanks %TRIMR removes trailing blanks
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE FullName = %TRIM(FirstName) + + %TRIM(LastName); /END-FREE
Programming in RPG IV Third Edition 13
%SUBST
Extracts a portion of a character string
%SUBST on right side on expression
Three arguments
String from which extraction is to occur Position within that string where substring is to start Length of substring (optional)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE Phone = 9705551212; AreaCode = %SUBST(Phone:1:3); // AreaCode = 970 Exchange = %SUBST(Phone:4:3); // Exchange = 555 Local = %SUBST(Phone:7); // Local = 1212 %SUBST(Phone:1:3) = 616 ; // Phone = 6165551212 /END-FREE
Programming in RPG IV Third Edition 14
%CHAR
%CHAR converts expression to character format
Usually used with numeric or date data Date format required for date data
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE Message = Today is + %CHAR(TodaysDate:*USA) + .; Message = You have earned + %CHAR(Points) + Frequent Flier Miles. /END-FREE
Programming in RPG IV Third Edition 16
Error Handling
Without explicit error handling within your program, any runtime error will cause the system to suspend the program Three methods:
%ERROR function for individual code lines MONITOR operation for blocks of code *PSSR subroutine for entire programs
17
%ERROR
Performs error handling for a single operation Code the (E) error handling extender with those operations that permit such an entry
If %ERROR is *ON following such an operation, perform an error routine
%ERROR
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE READ(E)SalesFile; SELECT; WHEN %ERROR AND %STATUS(SalesFile) = 01218 ; EXSR LockedRec; WHEN %ERROR; EXSR FileErr; WHEN %EOF(SalesFile); *INLR =*ON; RETURN; OTHER; EXSR Process; ENDSL; /END-FREE
19
Code within block does not require (E) extender If an error occurs within group, ON-ERROR operations specify error handling
ON-ERROR codes to identify errors
Similar to %STATUS
21
*PSSR Subroutine
Performs default error handling for a program
Automatically receives control when a program error occurs
Points to Remember
%SIZE, %LEN, %ELEM inspect or change data item attributes TEST operation checks date fields for validity
Can also check numeric and character fields for value date values
Points to Remember
Concatenation (+), %TRIM, and %SUBST perform string handling functions %DEC, %INT, and %UNS functions convert numeric expressions to a specific data representation %CHAR function converts numeric or date expressions to character
Programming in RPG IV Third Edition
24
Points to Remember
The %ERROR function, along with the (E) extender provide error handling capability for a single operation MONITOR and ON-ERROR handle errors that occur within a block of code *PSSR subroutine handles general program errors
Programming in RPG IV Third Edition
25