Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COBOL keyword REDEFINES

Tags:

cobol

May I ask, what is the usage of the keyword REDEFINES in COBOL? I can not understand the manual's definition.

What is the meaning of the following code?

 01 WS_CHARGE_TXT_8X                             PIC X(08) VALUE "10000000".  
 01 WS_CHARGE_NUM_8 REDEFINES WS_CHARGE_TXT_8X.  
     05 WS_CHARGE_8                               PIC 9(05)V9(03).  

Thank you!

like image 287
lamwaiman1988 Avatar asked Mar 11 '11 06:03

lamwaiman1988


People also ask

What is the difference between renames and redefines in COBOL?

Re: Difference between RENAMES and REDEFINES 01 TS-DATA. 05 VAR1 PIC X(5). 05 VAR2 REDEFINES VAR1 PIC 9(5). RENAMES:Regroup some of the Elementary-data-item in a GROUP.It gives this ele-data-item a new name can be operate more convenience.

What is correct about redefines clause?

The REDEFINES clause allows you to use different data description entries to describe the same computer storage area. ( level-number , data-name-1 , and FILLER are not part of the REDEFINES clause, and are included in the format only for clarity.)

Can we use redefines in file section?

REDEFINES is allowed in a level 01 entry in the File Section, but it will generate a warning message. The number of character positions described by prev-data-name need not be the same as the number of character positions in the subject of the REDEFINES clause.

Can we use redefines occurs clause?

Re: Redefines clause in occurs clause WE CAN REDEFINE A DATA ITEM HAVE AN OCCURs CLAUSE. but we cannot redefine a data item having occurs clause.


2 Answers

Basically Redefines reuses the spaces so in the above example WS_CHARGE_TXT_8X and WS_CHARGE_8 will point to the same block of memory. This allows you to look at a block of memory in different ways, in this case the variable can be viewed as a text PIC X and a signed numeric PIC S9. The -8X to -8 in the variable name is just stylistic to indicate the variable is being recast to another type or format to other programmers.

In the above example

  • the value of WS_CHARGE_TXT_8X is "10000000"
  • the value of WS_CHARGE_8 is of 10000.000.

If you moved 123.456 to WS_CHARGE_8 the value of WS_CHARGE_TXT_8X "00123456".

A more useful example is

  03 Birth-Date-YYYYMMDD    pic 9(8).
  03 filler redefines Birth-Date-YYYYMMDD.
     05 Birth-Date-YYYY     pic 9(4).
     05 Birth-Date-MM       pic 99.
     05 Birth-Date-DD       pic 99.

In this case you can access the whole date Birth-Date-YYYYMMDD or the year / month / day individually (Birth-Date-YYYY etc).

like image 171
Bruce Martin Avatar answered Nov 02 '22 23:11

Bruce Martin


It is analogous to union in 'C'. It saves working storage space, and MOVE statements, and is also useful for transposing arrays of PIC(X), or accessing repeated fields as an array. In the OP's case a numeric "type" is being created for the char contents of the prototype field.

like image 28
mckenzm Avatar answered Nov 02 '22 23:11

mckenzm