View previous topic :: View next topic
|
Author |
Message |
sibi Yohannan
New User
Joined: 15 Apr 2009 Posts: 47 Location: Bangalore
|
|
|
|
Hi,
I am seeing two types of initialization in COBOL programs, Both are compiling fine and do not gave any issues so far as per my knowledge.
01 TABLE-WS
05 TABLE-OCCURS OCCURS 2000 TIMES
INDEXED BY IND1.
10 A-WS PIC X(01)
One way
INITIALIZE TABLE-WS
Other way
Move SPACE to A-WS(IND1)
In a Loop 2000 times.
Is there any difference in these two way of initialization methods and which is the correct way of doing it?
Please advice |
|
Back to top |
|
|
Binop B
Active User
Joined: 18 Jun 2009 Posts: 407 Location: Nashville, TN
|
|
|
|
Hi Sibi,
as per my knowledge...
its both the same for the example specified ... though I would suggest the INITIALIZE statement as it is readable and helpful during maintenance. |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
If you MOVE SPACES to the TABLE-WS "01" level, then the compiler will generate an MVCL and clear the entire table to SPACES.
This should be the most efficient method, although internally, COBOL may also issue an MVCL if you specify an INITIALIZE. You'd have to compile your program, specifying the LIST,NOOFFSET compile options, to view what the compiler generates.
I would avoid initializing each individual table-entry (1-Byte) via a LOOP. This is not very efficient.
Bill |
|
Back to top |
|
|
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 283 Location: chennai
|
|
|
|
Quote: |
MOVE SPACES to the TABLE-WS "01" level, |
Thats the most efficient one. But, it works as expected only when the table contains alphabetic/alphanumeric items.
Quote: |
MOVE ZEROS to the TABLE-WS "01" level, |
would work for numeric data type.
For a table containing both numeric and alphabetic items, we
Code: |
MOVE dummy-arr-01 to Real-arr-01 |
Where dummy-arr is identical to real-arr but has values spaces for alphbetic.alpha numeric and zeroes for numeric. We do not mind the additional storage required for dummy-array.
Code: |
01 dummy-arr-01
05 dummy-arr occurs 10 times
10 dummy-a pic X(10) value spaces.
10 dummy-9 pic 9(10) value zeroes.
01 real-arr-01
05 real-arr occurs 10 times
10 real-a pic X(10) value spaces.
10 real-9 pic 9(10) value zeroes.
|
|
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Depending on the data-type, SPACES may not be appropriate. But in the example given by the original poster, his OCCURS 2000 Array consists of one-byte of PIC X(01).
It's really up to the reader to make that determination as to what is appropriate. But the MVCL will work every time for Array-Elements which are all PIC X (SPACES) or are all PIC 9 (ZEROS).
This is a basic level of knowledge which should not have to be explained in great detail. |
|
Back to top |
|
|
Binop B
Active User
Joined: 18 Jun 2009 Posts: 407 Location: Nashville, TN
|
|
|
|
Well thats why i felt INITIALIZE is better. it will take care of all the different data types and initialize all the fields accordingly in just a single statement... |
|
Back to top |
|
|
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 283 Location: chennai
|
|
|
|
Quote: |
But in the example given by the original poster, his OCCURS 2000 Array consists of one-byte of PIC X(01).
|
Yes. Of course.
But I was just trying to extend the scenario and share the method we follow. |
|
Back to top |
|
|
sibi Yohannan
New User
Joined: 15 Apr 2009 Posts: 47 Location: Bangalore
|
|
|
|
I was under the impression that, the correct way of initializing a Table is to initialize all the occurances individually.
So INITIALIZE command at the 01 Level of the Table will do the puprose effectively.
Thanks for the information. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
I was under the impression that, the correct way of initializing a Table is to initialize all the occurances individually. |
Why? As long as the entire area has the proper starting value, why might it matter if "all" were done or if individuals were done. . .?
Quote: |
So INITIALIZE command at the 01 Level of the Table will do the puprose effectively. |
As will the MOVE SPACES in this case. |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Dick,
I don't know what would go on "under the covers" regarding the INITIALIZE of the "01" level and that's why I suggested to the OP to compile the program with LIST,NOOFFSET to see what the compiler generates.
Who knows, it might be more draconian than what we're anticipating or it could be as simple as the MOVE LONG?
I've seen some INITIALIZE code translate into some wild BCT's and other forms of slipping itself into unconsciousness and it wouldn't surprise me if this type of code is generated.
This is why we can be assured that the MOVE SPACES at the "01" level will generate an MVCL.
Regards,
Bill |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Quote: |
This is why we can be assured that the MOVE SPACES at the "01" level will generate an MVCL. |
Yup, for sure. And yup, there has been some rather strange utc (under-the-covers) code generated. . .
I was just questioning why doing them one at a time (individually) might be "more correct"? |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
If the table needs initializing only once, an alternative would be:
Code: |
01 TABLE-WS.
05 TABLE-OCCURS OCCURS 2000 TIMES
INDEXED BY IND1.
10 A-WS PIC X(01) VALUE SPACE. |
No procedure division statements are necessary. See table handling in the manual. |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Terry,
Ya know, sometimes the easy stuff is right on the end of your nose, but in this case, it missed everyone's nose and perhaps was lodged in a place where the sun don't shine.
IIRC, the VALUE clause for array/table elements was introduced with either VS/COBOL II or COBOL/370.
Regards,
Bill |
|
Back to top |
|
|
|