Lesson 2. Exercises.

We propose you now to practice most of the tracing options we have discussed in this lesson.  To make it easy, we provide the TRACTEST EXEC © procedure. This procedure is listed below.  It has no other practical value than to learn about tracing.  It uses variables, arrays, host commands, subroutines, loops, etc. to enable you to exercise the different trace options.  The procedure should not contain errors.

The command format for the procedure is:

     TRACTEST [t-parm]

where the t-parm is a valid tracing option.

These are the steps we suggest for this exercise:

  1. Spool your console to your reader (CP SPOOL CON START *) in order to be able to collect the results.
  2. Have a look at the procedure listed below and try to understand the logic.
  3. Issue the TRACTEST command without any parameter.  This shows the results of the procedure without doing any tracing (default trace N).
  4. Issue the TRACTEST command with the different tracing options.  Try also to practice the interactive tracing (? prefix), for some of the options.
  5. While using interactive tracing, try to dynamically modify the tracing or the flow of the program.
  6. When you have finished your practice, stop your console spooling (CP SPOOL CONS STOP CLOSE), and receive the console listing from your reader to your A-disk as TRACTEST LISTING.  Edit the file and review it to see if you have understood everything.

This is the procedure:

 /***********************************************************************/
 /* Procedure TRACTEST                                                  */
 /* Format : TRACTEST [tracing-option]                                  */
 /***********************************************************************/
 address command
 parse upper arg traceopt .
 traceopt=check_parm(traceopt)
 trace value(traceopt)
 array.=0                        /* initialize array to zero */
 do i=1 to 6 by 2                            /* odd elements */
    array.i=i                                  /* value of i */
    j=i+1
    array.j=i**2
 end
 do i=1 to 6 by 2
    call output i
 end
 exit
 /***********************************************************************/
 /* Subroutine OUTPUT to produce an output on the terminal              */
 /***********************************************************************/
 OUTPUT:
    parse arg parm
    elem=parm+1
    'QUERY TIME (STACK'
    parse pull . . time . day date
    parse pull .
    say 'At' time 'on' day date ', the square of' parm 'is' array.elem
    return
 /***********************************************************************/
 /* Subroutine EMSG and EXIT                                            */
 /***********************************************************************/
 EXIT:
 EMSG:
  'DROPBUF 0'
  parse source . . myname .
  parse arg retc,errmsg
  if errmsg<>'' then say myname':' errmsg
  exit retc
 /***********************************************************************/
 /* Subroutine CHECK_PARM - checks validity of trace option given by    */
 /* the user.                                                           */
 /***********************************************************************/
 CHECK_PARM:
   c1=left(arg(1),1)
   if pos(c1,'?]')=0 then do;trpre='';tropt=c1;end
                     else do;trpre=c1;tropt=substr(arg(1),2,1);end
   if pos(tropt,'ACEFILNORS ')=0 then
      call emsg 8,"Invalid trace option '"arg(1)"' specified"
   if tropt=' ' then tropt='N'  /* default */
   return trpre||tropt

As an optional exercise on tracing, perform the following:

Write a small procedure that reads a file, extracts some data, performs a calculation on the data and writes the result back to another file.

Don't spend too much time on this exercise.  If you have too much difficulties with it, then our later lessons will definitely help you a lot, as we will learn how to read files. 

Then, you could give the next exercise a try...

  1. Get the file PRICE LIST ©  The layout of the records is as follows:
        01-07 Partnumber
        09-40 Description
        42-50 List price
    
  2. Write a procedure named VAT EXEC that
  3. You will encounter at least one problem with one record containing invalid data.  Use debugging techniques to discover why it fails.

When you have coded your version, take the time to review our commented solutions.

Our final exercise lets you also read a file and manipulate the data in it.  A brief introduction is however required.

You may already have seen so called one-lined procedures on some VM systems or packages.  A one-lined procedure is a procedure where:

  1. spaces and comments are eliminated
  2. all records are concatenated together in one or a few records.

If you want to have an example, look at the file ONELINED XEDIT ©.

This technique had a few advantages:

These advantages have less importance nowadays, as:

Note: Comments and spaces can also be eliminated via the EXECUPDT procedure.

Now, before you code the procedure to manipulate such a file, try to answer these few questions:

Question 17

Can you say what you would have to do to recreate a multi-line exec from a one-lined procedure ?  Can you give at least one method by which you can achieve the results ?

Can you imagine a reason why this would not be a simple job to make it foolproof ?

Although CMS pipelines can do it in one command, as follows:

    PIPE < ONELINED EXEC ! SPLIT ; ! > MULTILIN EXEC A

we propose you to write a procedure that converts a one-lined exec into a multi-lined exec.  To that purpose, we provide the MULTILIN SKELETON © that you have to complete.  This procedure uses EXECIO to read and write the files.  Review the comments in the procedure to see what you have to do.

You could test your procedure on ONELINED XEDIT ©.  but this file is rather large and if you have to trace your procedure, it would take you too long.  We therefore also provide ONELINED EXEC © which contains only a few statements.

When you have coded your procedure, you can review our commented solutions.

This concludes the exercises for Lesson 2.  Lesson 3 will discuss parsing and file I/O techniques.