Transcript RPG IV - Chapter 11
Advanced Functions and Error Handling
Chapter 12
Presentation © Copyright 2002, Bryan Meyers
Objectives
• Understand functions to inspect or manipulate data items • Work with character strings • Discuss error handling techniques
Programming in RPG IV Third Edition 2
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 • Checks character and numeric fields for valid date/time data – Requires operation code extender (D/T/Z) – Requires format in Factor 1 (*ISO default)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D TodaysDate S D D UserDate S 8 0 INZ(‘20030315’) /FREE TEST(E) TodaysDate; TEST(DE)*ISO UserDate; /END-FREE Programming in RPG IV Third Edition 8
%SCAN
• Looks for a character or string in a character field – Returns an integer indicating position of character • Case sensitive • Blanks are not ignored
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE Position = %SCAN(‘/’:FullName); // Search for slash (/) in FullName Position = %SCAN(FieldA:FieldB); // Search for FieldA in FieldB Position = %SCAN(FieldA:FieldB:3); // Start search in position 3 /END-FREE Programming in RPG IV Third Edition 9
%CHECK
• Returns location of mismatch between listed characters and compare field – Signals absence of a base character from set of compare characters • Checks left to right – %CHECKR checks from right to left
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE InvalPos = %CHECK(Alpha:FieldA); // Check FieldA for characters in Alpha Length = %CHECKR(' ':Name); // Find rightmost nonblank character /END-FREE Programming in RPG IV Third Edition 10
Field Character Manipulation
• + (Concatenate Character Strings) • %TRIM functions • %SUBST (Get substring) function • %DEC (Convert to packed decimal) function • %INT (Convert to integer) function – %UNS (Convert to unsigned integer) function • %CHAR (Convert to character) function
Programming in RPG IV Third Edition 11
Character String Concatenation
• Using the + operator with EVAL is a convenient way to concatenate two (or more) strings to form a new string
*.. 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 • Inserts characters into a character substring – %SUBST on left side of 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
%DEC, %INT and %UNS
• %DEC converts expression to packed decimal format – Indicate precision and decimals in 2 nd /3 rd arguments – %DECH half adjusts (rounds) result • %INT converts expression to integer Format) – %INTH rounds result • %UNS converts expression to unsigned integer – %UNSH rounds result
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE Result = %DECH(Hours * Rate:7:2); Area = %UNSH(3.14159 * Radius**2); /END-FREE Programming in RPG IV Third Edition 15
%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
Programming in RPG IV Third Edition 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 • May be used in conjunction with %STATUS function
Programming in RPG IV Third Edition 18
%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 Programming in RPG IV Third Edition 19
MONITOR and ON-ERROR
• Performs error handling for a block of code (“monitor group”) • MONITOR begins a monitor group – Ended with ENDMON • 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 – Only one ON-ERROR condition executes
Programming in RPG IV Third Edition 20
MONITOR and ON-ERROR
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 /FREE MONITOR; READ Sample; DOW NOT %EOF(Sample); CHAIN Samp OtherFile; EXSR Calcs; READ Sample; ENDDO; ON-ERROR 1218; EXSR LockedRec; ON-ERROR *FILE; EXSR FileErr; ON-ERROR *ALL; EXSR GenErr; *INLR =ON; RETURN; ENDMON; /END-FREE Programming in RPG IV Third Edition 21
*PSSR Subroutine
• Performs default error handling for a program – Automatically receives control when a program error occurs • To use the *PSSR for file errors, explicitly designate INFSR(*PSSR) – Can specify another subroutine for file errors
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 FFilename++IPEASFRlen+LKlen+AIDevice+.Keywords+++++++++++++++++++++++++++++ FSample UF E K DISK INFSR(*PSSR) Programming in RPG IV Third Edition 22
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 • %SCAN and %CHECK inspect the contents of character fields
Programming in RPG IV Third Edition 23
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