COBOL .

COBOL Initialize

Working-Storage for Example 1 and 2

000340 01  CAT-AND-DOG.
000350     05  CAT    PIC 9.
000360     05  DOG    PIC X.   

Initialize Example Number 1 - Initialize a PIC 9 and a PIC X field

004310
004320     INITIALIZE CAT.
004430     INITIALIZE DOG.
004440
This will move a 0 to CAT and a space to DOG.

Initialize Example Number 2 - Initialize a PIC 9 and a PIC X field

004310
004320     INITIALIZE CAT-AND-DOG.
004440
This will also move a 0 to CAT and a space to DOG.

Working-Storage for Example 3

000340 01  CATS-AND-DOGS-GROUP.
000350     05  CATS-AND-DOGS   OCCURS 10.
000350         10  CATS        PIC 9.
000360         10  DOGS        PIC X.   

Initialize Example Number 3 - Initialize a table

004310
004320     INITIALIZE CATS-AND-DOGS.
004440
This will move a 0 to all 10 CATS and a space to all 10 DOGS. Certainly a very handy way to initialize a table. NOTE: This verb can sometimes use a lot of CPU.
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by gbutler on 2016-02-24 Rate this Comment

Also (as TheMadProfessor points out), there is a way to force specific values into specific field types...

I use this on a regular basis when trying to dynamically get the number of occurences in a table/array. As opposed to having the value hard-coded (HARDCODE IS EVIL!!!)

As an example, given this array:
01 work-area.
05 ws-flda pic x(01).
05 ws-fldb pic 9(02).
etc..
05 ss pic s9(3) comp-3 value zeroes.
05 ws-fldx pic x.
05 array-with-no-parent-group occurs 27 times.
10 array-fld1 pic x(01).
10 array-fld2 pic 9(03).
05 ws-fld-following-the-array pic x(10)


The (OLD_SCHOOL norm) of controlling the bounds of this array in the procedure division would require hardcoding (either by having a WS field with a hardcoded value of 27, or worse yet in multiple places in the procedure like this:

perform
varying ss from 1 by 1
until ss is greater than 27
display "array-fld1:" array-fld1 (ss)
"array-fld2:" array-fld2 (ss)
end-perform.

So, if the number of occurs changes, you have to go through and find all the variations of the hardcoded limits (remember there are many ways to say the above, including (but not limited to) all of the following:
- equal 27 (to stop at the last occurence)
- greater than 26 (to stop at the last occurence)
- less than 28

The way I would do this (and now back on track of the INITIALIZE topic) is this.

add a WS field such as this.
05 ws-array-max-ss value s9(3) comp-3 value zeroes.

procedure division.
move spaces to array-with-no-parent-group.
initialize array-with-no-parent-group
replacing numeric data by 1.
* we have effectively just moved 1 to all occurences of array-fld2.
* the following (function logic and 'ALL' reserved-word) may vary based on compiler,
* but effectively adds all 27 occurences of array-fld2 (all of which have a value of 1)
compute ws-array-max-ss = (function sum (array-fld2(ALL)))
* now that we have our number of occurences, lets initialize it for later use...
initialize array-with-no-parent-group.

...

perform
varying ss from 1 by 1
until ss is greater than ws-array-max
display "array-fld1:" array-fld1 (ss)
"array-fld2:" array-fld2 (ss)
end-perform.

now, if the number of occurence change, there is no logic/control changes needed, as the varialbe that limits bounds control is calculate once at program execution...

Comment by gbutler on 2011-04-21 Rate this Comment

As a practice, if using to initialize a record area, I have always moved spaces to the record prior to the INITIALIZE as the initialize does not initialize FILLER areas (could be only on some compilers)

01 recorda.
05 key pic x(20).
05 data pic x(20).
05 filler pic x(40).

...

move spaces to recorda.
initialize recorda.

Comment by TheMadProfessor on 2010-03-09 Rate this Comment

Actually, INITIALIZE has an optional REPLACING clause (albeit one I've seldom seen used):

INITIALIZE identifier-1 REPLACING class-identifier BY identifier-2|literal-1

where class-identifier can be ALPHABETIC, APLHANUMERIC, NUMERIC, ALPHANUMERIC-EDITED, NUMERIC-EDITED, DBCS or EGCS. (The latter two are IBM-extensions, not standard.)

INITIALIZE field-name REPLACING ALPHANUMERIC BY '&' would work but not for NUMERIC. Not sure whether it would work for NUMERIC-EDITED, but I would suspect not. INITIALIZE field-name REPLACING NUMERIC BY 7 should work just fine...BY '7' might or might not, probably depends on the compiler.

Note that INITIALIZE does not look at REDEFINES fields (which makes sense, as there could easily be multiple ways to initialize a given memory location if it did); only the primary definition controls how a given area is initialized. Also, it will only alter named variables unless the WITH FILLER optional clause in included, IWC all FILLER fields will also be initialized as per their PICTURE clauses.

The comment about example 3 being wrong I believe is correct, but for a reason other than that stated. Since CATS-AND-DOGS occurs 10 times, I believe most compilers will toss an error as the example is coded (YMMV). A method that should work on any compiler:

PERFORM VARYING SUB1 FROM 1 BY 1 UNTIL SUB1 > 10
....INITIALIZE CATS-AND-DOGS(SUB1)
END-PERFORM

What I'm unsure of is what would happen if one did INITIALIZE CATS-AND-DOGS-GROUP. It may be compiler-dependent as to whether the elementary items within the OCCURS would be treated as a redefinition of CATS-AND-DOGS or not.

Comment by archive on 2013-02-22 Rate this Comment

Just wanted to write ;)

i think OCCURS can't be used in level 01

Comment by archive on 2013-07-25 Rate this Comment

Thanks for the site. It is very helpful & straightforward.

I have a question... How would an INITIALIZE statement look if I wanted to override the default value (zero or space) with a '&' ???

Comment by dsiebel on 2007-04-09 Rate this Comment

The Example 3 code is invalid: the OCCURS clause cannot be on the 01 level.

Comment by Larry on 2006-05-01 Rate this Comment

You can use initialize on group or non group items. Try it out, it's always possible that whatever compiler you are using doesn't accept it for some reason. I've only used initialize on IBM mainframe and compliant systems.

Initialize does make more sense on group items, on non group items it is probably more efficient and clear to move zero or spaces.

Comment by archive on 2006-05-01 Rate this Comment

The INITIALIZE statement may only be used on group items is this TRUE or FALSE? why?

Comment by archive on 2009-01-20 Rate this Comment

You might want to mention that use of the INITIALIZE statement on a variable that uses a VALUES clause will cast the type and not the content.

I see guys at my shop struggle with this daily.

Comment by archive on 2006-12-21 Rate this Comment

I was told to stop using this instruction because it was placing binary zeroes in some fields.

I this true? I've never heard it before...

 
Sign in to comment on COBOL Initialize.