View previous topic :: View next topic |
Author |
Message |
sunnys_day Beginner
Joined: 17 Feb 2003 Posts: 1 Topics: 1
|
Posted: Wed Jun 11, 2003 12:31 am Post subject: Array initialisation in COBOL. |
|
|
Consider the following array.
01 working-storage.
03 array1 occurs 10 .
05 ele1 pic 9.
05 ele2 pic x.
03 other-element pic 9(2).
Now, suppose that I want to initialise only array1, how do i got about it?
Pls note that I do not want to initialise other-element.
I tried
INITIALIZE array1.
However, this gives an error on the lines of "Trying to access an array without mentioning the subscript."
A work around would be as following:-
Perform varying ws-no 1 by 1 until ws-no>10
Initialize array1(ws-no)
End-Perform.
However, I do not want to use this.
Could you pls suggest an alternative. |
|
Back to top |
|
|
satjag Beginner
Joined: 19 Dec 2002 Posts: 19 Topics: 2
|
Posted: Wed Jun 11, 2003 12:41 am Post subject: |
|
|
Hello,
It is not possible to access an array or its element without using the subscript.The method you have suggested is the usual method used for initialising the array elements. _________________ Regards,
satjag |
|
Back to top |
|
|
Tophe Beginner
Joined: 02 Dec 2002 Posts: 8 Topics: 0 Location: Tours, France
|
Posted: Wed Jun 11, 2003 12:58 am Post subject: |
|
|
You can also rewrite your WSS like this :
01 working-storage.
03 table.
05 array1 occurs 10 .
07 ele1 pic 9.
07 ele2 pic x.
05 other-element pic 9(2).
and you can use INITIALIZE TABLE.
HTH
Chris |
|
Back to top |
|
|
vp Beginner
Joined: 30 Jun 2003 Posts: 9 Topics: 0
|
Posted: Mon Jun 30, 2003 11:45 am Post subject: |
|
|
If you don't want your other-element to be initialized make it a 03 level (same as table). |
|
Back to top |
|
|
CaptBill Beginner
Joined: 02 Dec 2002 Posts: 100 Topics: 2 Location: Pasadena, California, USA
|
Posted: Mon Jun 30, 2003 2:43 pm Post subject: |
|
|
Or you could say
Code: | MOVE '0 0 0 0 0 0 0 0 0 0 ' TO TABLE. | as defined by Tophe. |
|
Back to top |
|
|
slade Intermediate
Joined: 07 Feb 2003 Posts: 266 Topics: 1 Location: Edison, NJ USA
|
Posted: Mon Jun 30, 2003 6:06 pm Post subject: |
|
|
Or you could code:
Code: |
03 table value '0 0 0 0 0 0 0 0 0 0 ' .
|
if you only have to init once.
Also, I think you can now value each element definition in the array. Don't know what COBOL Std Version started that.
Regards, Jack.[/quote] |
|
Back to top |
|
|
Meera Beginner
Joined: 27 Jun 2003 Posts: 7 Topics: 0
|
Posted: Tue Jul 01, 2003 11:16 am Post subject: |
|
|
Why bother? rewrite your fields:
01 working-storage.
02 init-array.
03 num-1 pic 9.
03 alpha-1 pic x.
03 num-2 pic 9.
03 alpha-2 pic x.
03 num-3 pic 9.
03 alpha-3 pic x.
03 num-4 pic 9.
03 alpha-4 pic x.
03 num-5 pic 9.
03 alpha-5 pic x.
03 num-6 pic 9.
03 alpha-6 pic x.
03 num-7 pic 9.
03 alpha-7 pic x.
03 num-8 pic 9.
03 alpha-8 pic x.
03 num-9 pic 9.
03 alpha-9 pic x.
03 num-10 pic 9.
03 alpha-10 pic x.
02 filler redefines init-array.
03 array1 occurs 10 .
05 ele1 pic 9.
05 ele2 pic x.
02 other-element pic 9(2).
and write
initialize init-array replacing numeric by zero alphanumeric by spaces. |
|
Back to top |
|
|
Bithead Advanced
Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Tue Jul 01, 2003 1:41 pm Post subject: |
|
|
Here is a bithead way to do it.
03 table.
05 array1 occurs 10 .
07 ele1 pic 9.
07 ele2 pic x.
03 r-1 redefines table.
07 r-1-a pic x(18).
07 filler pic x(2).
03 r-2 redefines table.
07 filler pic x(2).
07 r-2-a pic x(18).
move 0 to ele1 (1).
move ' ' to ele2 (1).
move r-1-a to r-2-a. |
|
Back to top |
|
|
slade Intermediate
Joined: 07 Feb 2003 Posts: 266 Topics: 1 Location: Edison, NJ USA
|
Posted: Tue Jul 01, 2003 3:41 pm Post subject: |
|
|
Meera,
Why bother what?
Bithead,
What's so cool about r-1-a and r-2-a?
Regards, Jack. |
|
Back to top |
|
|
Bithead Advanced
Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Tue Jul 01, 2003 3:58 pm Post subject: |
|
|
3 lines of executable code - high performance. Try defining a variable with all the initial values for element 1 then this is reduced to 2 lines of executable code.
03 init-values.
07 filler pic 9 value 0.
07 filler pic x value ' '.
move init-values to array1 (1).
move r-1-a to r-2-a.
Any table, any size. Can't get any faster. |
|
Back to top |
|
|
|
|