Thursday, January 17, 2008

Little Know Debugging Tricks:

%SUBSTR Built-In Function
This function is great when you're working with large strings. You can view a segment of a string as shown below (assume that StringFldA = 'abcdefghijklmnopqrstuvwxyz'):

Debug command: EVAL %SUBSTR(StringFldA 12 5)
Displayed result: %SUBSTR(StringFldA 12 5) = 'lmnop'

This is convenient when working with larger string fields. You can also use the %SUBSTR function to set the value of a specific portion of a string.
Debug command: EVAL %SUBSTR(StringFldA 12 5) = 'xxxxx'
Displayed result: %SUBSTR(StringFldA 12 5) = 'xxxxx'
Debug command: EVAL StringFldA
Displayed result: StringFldA = 'abcdefghijkxxxxxqrstuvwxyz'

You can also use %SUBSTR to set a conditional breakpoint or watch condition. For example, the following code would stop execution only when positions 12 thru 16 of StringFldA are 'xxxxx':

Debug command: BREAK 100 when %SUBSTR(StringFldA 12 5) = 'xxxxx'

Or you could watch for those same positions to change by using this watch condition:

Debug command: WATCH %SUBSTR(StringFldA 12 5)

This way, anytime the contents of positions 12 thru 16 change, program execution stops and you are notified.
%INDEX Built-In Function
The %INDEX function is handy when you're using multiple-occurrence data structures. It's also useful in combination with _QRNU_DSI_xxxx (where xxxx = the name of a multi-occurrence data structure). The command EVAL _QRNU_DSI_xxxx returns the current occurrence of a multiple-occurrence data structure. Using the %INDEX function will change the current occurrence. See the example below:
d WorkDS1 ds occurs(3)
d StringA 10a
d StringB 25a
Debug command: EVAL _QRNU_DSI_WorkDS1
Displayed result: 1 (or whatever the current occurrence of WorkDS1 is)

Debug command: WorkDS1 = %INDEX(3)
Displayed result: WorkDS1 = %INDEX(3) = 3
Interrogated subfields will now reflect the values the of third occurrence of the data structure.
Parting Shot
Sometimes many of the field names are really long, so if you don't type well (or just don't like to type), then using the EQUATE debug command can help you. This command allows you to define an "alias" for a field name, expression, or command. For instance...

Debug command: EQUATE SmName This_is_a_really_long_field_name

You can then find the value of "This_is_a_really_long_field_name" by keying the command shown below:

Debug command: EVAL SmName

The EQUATE command can also be used to create a macro of sorts. This is done by assigning an alias to a complete command. Here's an example:

Debug command: EQUATE SmCmd EVAL %substr(StringA,5,5)

By keying SmCmd and pressing Enter, you can display the value of positions 5 thru 9 of the StringA variable.

No comments: