Mainframes 360
The one stop destination for System Z professionals

Saturday, April 5, 2014

DFSORT and amazing new data-processing tricks

DFSORT and amazing new data processing tricks

In a perfect world, data columns would be neatly aligned and easy to sort, edit and display. But here, in the real world, data can get unorderly and messy! DFSORT is a high-performance sort, merge, copy and data-processing tool.

Let's establish an input file BOOK.ORDERS, we would like to process in our example.
DFSORT is adept at helping you sort the data. The SORT FIELDS=(p,m,f,s) control statement can sort records by various data columns.
DFSORT can filter and limit the records in the output. The INCLUDE COND=(column1,operator,column2) and INCLUDE COND=(column1,operator,constant) tests allow certain records to be kept. In contrast, the OMIT COND statement deletes records.

Select the orders on computer books:
Select the orders with costs between $2000 and $3000:
Select the orders that are 90 days and older:
DFSORT can sum records. are added together. SUM FIELDS=(p,m,f) will treat records with the same sort or merge keys as one whole and add them up.

Total order costs breakup by subject-area:
DFSORT can reformat records. Reformat is possible at pre- and post- processing stages through INREC and OUTREC FIELDS respectively. INREC and OUTREC build the records item by item.

Report containing book-title, followed by a space and then order-cost in $Z,ZZ9.99 format:
Did you know that DFSORT has many new tricks up its sleeve to perform these feats and much more.
  • Overlay specific parts of records
  • Parse delimited fields
  • Justify data
  • Test for numeric data
  • Handle numeric values that have separators and decimal points
  • Conversion to another format
  • Reformat different records in different ways
  • Add sequence numbers
In this article, I will share some of my favourite DFSORT tricks.

Abracadabra! Overlay specific fields

OVERLAY is a new parameter that can be used on INREC, OUTREC and OUTFIL statements to change data in specific columns, without affecting the rest of the record. OVERLAY can also be used add fields at the end of the record. With OVERLAY, you can concentrate on just the fields on interest without building the entire record item-by-item.

Earlier overlays can be seen by later overlays. A single OUTREC OVERLAY can be extremely powerful!

Wednesday, April 2, 2014

ODO Tables and Dynamic Arrays

ODO Tables

Fixed-length tables are good, if the table size is known to the programmer. At times, it is hard to predict an accurate table size. In such situations, COBOL's ODO clause or OCCURS DEPENDING ON may be used to create a table of size X, where X is a variable. Further, OCCURS DEPENDING ON does not offer any memory savings. ODO tables are always allocated at the maximum size N at compile time, similar to fixed tables. But during SEARCH or SEARCH ALL, ODO tables limit the access to just X out of N total occurrences (X < N), thereby saving processing time.
       DATA DIVISION.                                                   
  WORKING-STORAGE SECTION.
  *______________________________________________________________*
  * MISCALLANEOUS VARIABLES *
  *______________________________________________________________*
  01 WS-VARIABLES.
  05 X PIC S9(04) COMP.
  *______________________________________________________________*
  * INTERNAL TABLES/ARRAYS *
  *______________________________________________________________*
  01 CUSTOMER-ACCOUNTS-TABLE.
  05 CUSTOMER-ACCOUNT OCCURS 1 TO 10 TIMES
  DEPENDING ON X.
  10 CASH-ACCOUNT-NO PIC X(10).
  10 BALANCE PIC 9(07)V99.
  10 CUSTOMER-NAME PIC X(30).
CUSTOMER-ACCOUNT is said to be the ODO subject, and X the ODO object. If X is assigned a value 5 at run time, it limits access to 5 occurrences of CUSTOMER-ACCOUNT.


Dynamically allocated Arrays

In COBOL, the compiler estimates the memory needs for all WORKING STORAGE variables before execution. But, there may be cases when the memory needs cannot be established at compile-time. For example, you may want an array large enough to hold a file's records. In such cases, programs need to dynamically allocate memory.

Dynamic memory is allocated using a special LE(Language environment) service CEEGTST (Get Storage). The dynamic memory requested by the program comes is allocated by the system from the heap. Memory allocated using CEEGTST can be grown, resized through CEECZST (Change Storage). When memory is no longer needed, it is cleaned up, recycled using CEEFRST (Free Storage).

A dynamic array is allocated at run-time. First, the array layout is defined in LINKAGE SECTION. Like all LINKAGE SECTION items, the array layout does not occupy memory space. Rather, it will be used to address blocks of memory defined elsewhere in external storage.
       LINKAGE SECTION.                                                 
  01 LS-PARM-DATA.
  05 LS-PARM-LEN PIC S9(04) COMP.
  05 LS-ARRAY-LEN PIC S9(04) COMP.
 
  *______________________________________________________________*
  * DYNAMIC ARRAY *
  *______________________________________________________________*
  01 CUSTOMER-ACCOUNTS-TABLE.
  05 CUSTOMER-ACCOUNT OCCURS 1 TO 10 TIMES
  DEPENDING ON LS-ARRAY-LEN.
  10 CASH-ACCOUNT-NO PIC X(10).
  10 BALANCE PIC 9(07)V99.
  10 CUSTOMER-NAME PIC X(30).
Next, CEEGTST routine is called for dynamic memory allocation. CEEGTST accepts the HEAP-ID-0: the heap id and HEAP-SIZE: the amount of memory needed as inputs. If the memory requested is allocated, the HEAP-ADDRESS that points to the chunk of space allocated and FEEDBACK code indicating success or error is are returned as outputs.
       1100-ALLOC-MEMORY.                                              
  ****
  * THIS ROUTINE USES CEEGTST LE SERVICE TO ACQUIRE STORAGE *
  * DYNAMICALLY. ALL REQUESTS FOR STORAGE ARE CONDITIONAL. IF *
  * THE STORAGE IS NOT AVAILABLE, THE FEEDBACK CODE(FC) IS SET & *
  * RETURNED TO YOU, BUT THE PROGRAM DOES NO ABEND. *
  * *
  * CALL LE-GET-STORAGE USING HEAP-ID-0 SIZE ADDRESS FEEDBACK *
  ****
  MOVE ZERO TO HEAP-ID-0
  COMPUTE HEAP-SIZE = WS-ELM-SIZE * LS-ARRAY-LEN
 
  CALL LE-GET-STORAGE USING HEAP-ID-0
  HEAP-SIZE
  HEAP-ADDRESS
  FEEDBACK
  IF CEE000 OF FEEDBACK
  MOVE 'Y' TO WS-STORAGE-REQUEST-SW
  ELSE
  MOVE 'N' TO WS-STORAGE-REQUEST-SW
  END-IF.
  1100-EXIT. EXIT.

HEAP-SIZE is computed as Array length x size of 1 array element. The HEAP-ADDRESS contains the address of the newly allocated memory. The last step would be to establish addressability, by setting CUSTOMER-ACCOUNTS-TABLE to point to the newly allocated memory at HEAP-ADDRESS. CUSTOMER-ACCOUNTS-TABLE layout effectively is simply a redefinition for that memory block.
       1000-INITIALIZE.                                              
  PERFORM 1100-ALLOC-MEMORY THRU 1100-EXIT.
  IF STORAGE-REQUEST-SUCC
  SET ADDRESS OF CUSTOMER-ACCOUNTS-TABLE TO HEAP-ADDRESS
  END-IF.
  1000-EXIT. EXIT.

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.