Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use a REDEFINE clause..

Status
Not open for further replies.

soulcontroller

Programmer
Mar 13, 2003
1
US
I had a question regarding the REDEFINE clause, I know how to code it:
01 SALES-REC-1.
05 SPERSON-NO-1 PIC X(5).
05 SPERSON-NAME-1 PIC X(20).
05 NO-REC-GROUP PIC 9(3).
05 SALES-AMT REDEFINES NO-REC-GROUP PIC 9(4)V99.

Below is the sample data, the number on the right 002 (no-rec-group) tells you how many records you have. The next two numbers (99999, 78912) are sales-amt. How do you read the correct data into the right field? 00002 JOHN JONES 002
00002 JOHN JONES 99999
00002 JOHN JONES 78912

I'm using NetExpress compiler on PC. Any help would be appreciated.
 
Your data discriptions do not match your data. Either SALES-AMT should have a PICture of 9(3)V99, or the data should have six digits. Also, the data shows a space between fields. If the data actually has these spaces, the data description should match it with FILLERs.

Here is how I would code it:
Code:
01  SALES-REC-1.
    05  SPERSON-NO-1                   PIC X(5).
    05                                 PIC X.
    05  SPERSON-NAME-1                 PIC X(20).
    05                                 PIC X.
    05  SALES-AMT                      PIC 9(3)v99.
    05  REDEFINES SALES-AMT.
        10  NO-REC-GROUP               PIC 9(3).
        10  NO-REC-GROUP-FLAG          PIC X(2).

    . . .

    IF NO-REC-GROUP-FLAG = SPACE
        it is a group record
    ELSE
        it is a sales record
    END-IF
 
soul-

Your REDEFINES is not correct:
Code:
     01  SALES-REC-1.
         05  SPERSON-NO-1                   PIC X(5).
         05  SPERSON-NAME-1                 PIC X(20).
         05  NO-REC-GROUP                   PIC 9(3).
         05  SALES-AMT REDEFINES NO-REC-GROUP   PIC 9(4)V99.
[\code]
The redefining item should be the same size as the item it redefines.  E.g.
[code]
     01  SALES-REC-1.
         05  SPERSON-NO-1                   PIC X(5).
         05  SPERSON-NAME-1                 PIC X(20).
         05  NO-REC-GROUP.
             10  NO-REC                     PIC 9(3).
             10                             PIC X(3).
         05  SALES-AMT REDEFINES NO-REC-GROUP   PIC 9(4)V99.
[\code]

Regards.

Glenn
 
webrabbit and 3gm are on the right track with their definitions. The last question you had about how do you read the data into the right field indicates there might be some misunderstanding about redefines.

The redefines causes the second field to describe the same area of memory. The data is in "both" fields since they define the same location in memory. It is up to your program logic to figure out which definition is the right one to use for that record. Webrabbit was on the right track with his approach. He checked the 2 bytes that follow the 3-digit number. If those are spaces, he assumed he was dealing with the 3-digit number. If not, he assumed he was dealing with the 5-digit number. There's usually some sort of indicator on a record when you have something like this to tell you which is the correct definition to use.
 
I recently had a tech question regarding redefinition in Cobol.It asked - can redefining happen if
a) the item being redefined is smaller (than original field), and
b) if the item being redefined is larger (than original field) ?

Alan
 
Hi Alan,

It probably depends on the compiler vendor/version you're using. But, if I were you, I'd always equal the redefined item. Not doing so can lead to confusion (even if it's permitted) if you (or more importantly, someone who follows you) decide to "count bytes".

I know that IBM "MVS" compilers permit under redef with a warning. I think over redef was not, until recently. The latest compiler version allocates enough bytes to the field to cover its largest designation.

HTH, Jack.
 
thanks, Slade
yes, I am using MVS cobol on IBM.
The question was presented to me in such a way that only 'yes' or 'no' could be answered, so I answered 'yes' for redefining if the field is smaller than the original, and 'no' if the redefining field was larger than the original field.

Alan
 
You can have a filler redefine the field and then split it up into subfields.
05 Filler Redefines xyz.
10 field-1 pic 9(03).
10 filler pic x(03).

If you move a group item to a group item everything is considered text and it doesnt matter what is in the field.

If you do not like my post feel free to point out your opinion or my errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top