When PG or MU is a descriptor

I would appreciate it if somebody can give me a quick example/s of how to read/access PG or MU fields if they are defined as descriptors?

Thank you,
Sam

This forum is a place to post code samples, not where you would request them. But it is an improvement over your previous placement.

Descriptors using components from MUs or PEs are painful to use. Most DBAs and developers avoid them. One reason is that you can’t use them in a READ statement. The following code won’t compile. You get

DEFINE DATA LOCAL
1 EMP1   VIEW EMPLOYEES
  2 CURR-CODE (2)
  2 SALARY    (2)
1 #CS
  2 #C (A3)   INIT <"USD">
  2 #S (P17)  INIT <32500>
                        1 REDEFINE #CS
  2 #CURR-SAL (A12)
END-DEFINE
READ EMP1 BY CURRENCY-SALARY FROM #CURR-SAL
  DISPLAY (SG=F)
          *COUNTER
          CURR-CODE (*)
          SALARY (*)
END-READ

You can use such a descriptor in a FIND, but that returns records in ISN sequence, which rarely matches key sequence.

FIND EMP1 WITH CURRENCY-SALARY = #CURR-SAL
  DISPLAY (SG=F)
          *COUNTER
          CURR-CODE (*)
          SALARY (*)
END-FIND
   CNT     CURRENCY  ANNUAL
             CODE    SALARY
---------- -------- ---------
 
         1 USD          32500
           USD          29900
         2 USD          32500
           USD          29900
         3 USD          42800
           USD          39300
         4 USD          32500
           USD          29900
         5 USD          36500
           USD          34600

Another issue is that a record can be returned multiple times from a FIND, leading to very complex application logic.

This type of descriptor can be used with HISTOGRAM.

1 EMP2   VIEW EMPLOYEES
  2 CURRENCY-SALARY     2 REDEFINE CURRENCY-SALARY
    3 #CURR (A3)
    3 #SAL (P17)
...
HISTOGRAM EMP2 FOR CURRENCY-SALARY FROM #CURR-SAL TO #CURR-SAL
  DISPLAY (SG=F)
          *COUNTER
          #CURR
          #SAL (NL=9)
          *ISN
          *NUMBER
END-HISTOGRAM
   CNT     #CURR   #SAL       ISN        NMBR
---------- ----- --------- ---------- ----------
 
         1 USD       32500          1          4
         2 USD       32500          3          1
         3 USD       32500          5          1

For a result that looks more like it was produced from a READ, you can embed a FIND within a HISTOGRAM. The HISTOGRAM provides the key sequence and the FIND reports the records for each of the HISTOGRAM’s descriptor values.

1 Like

Thank you Ralph. This was a great help.

There are uses for both MU descriptors and fields within a PE that are descriptors.

For example. You might have an MU field with magazines that people subscribe to. You want to FIND, in no particular order, all the records for people who subscribe to Sports Illustrated (perhaps you are thinking of producing a similar publication and want to get a feel for how many people subscribe to SI in a particular file).

By contrast, if you had a PE with one field called Subscription, and the subscriptions were “numbered” in sequence by how many years the people had each subscription; you would be in a position to find out (via HISTOGRAM) how many records had SI first, how many second, etc.

That said, the maintenance of PE’s requires a lot of careful planning. Whereas MU’s are designed for repeating data where position (subscript) is irrelevant, PE’s are designed to be effective when positioning is important.

I will check the Forum. I believe I posted an article about MU’s and PE’s.

1 Like