Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

How to define and access an array in COBOL

IBMMAINFRAMES.com Support Forums -> Mainframe COBOL

Author rajeevdas03 Warnings : 1


New User Posted: Wed Oct 04, 2006 11:02 am access an array in COBOL

Message
Post subject: How to define and

Joined: 21 Jan 2006 Posts: 9 Location: Mumbai

HOW TO DEFINE ARRAY IN COBOL. SUPPOSE I HAVE DEFINE NY ARRAY LIKE THIS 01 WS-TABLE ONE 05 WS-TABLE ONE OCCURS 10 TIMES PIC X(10). HOW CAN WE ACCES 5th ELEMENT OF THIS TABLE USING SUBSCRIPT AND INDEX? PLS GIVE ME EXAMPLE

Back to top

<>
DavidatK
Active Member Posted: Wed Oct 04, 2006 10:35 pm and access an array in COBOL Post subject: Re: How to define

Joined: 22 Nov 2005 Posts: 704 Location: Troy, Michigan USA

Rajeev, I'm not sure if I understand your syntax. Define and use a cobol table like this:
Code: 01 WS-TABLE. 05 WS-TABLE-ENTRY PIC X(10) OCCURS 10 TIMES. 05 WS-TABLE-SUBSCRIPT PIC S9(3) COMP-3. MOVE 5 TO WS-TABLE-SUBSCRIPT. DISPLAY WS-TABLE-ENTRY(WS-TABLE-SUBSCRIPT). (or) DISPLAY WS-TABLE-ENTRY(5).

Dave
Back to top MFRASHEED
Active User Posted: Wed Oct 04, 2006 11:27 pm and access an array in COBOL Post subject: Re: How to define

Joined: 14 Jun 2005 Posts: 187

To answer other part of your question using INDEX. Following is an example:


Code: 01 TEST-TABLE. 05 TEST-TAB

OCCURS 10 TIMES INDEXED BY INDX-1. 10 TEST-DAT1 PIC X. 10 TEST-DAT2 PIC X.

To refrence a particular element in table use SET clause


Code:

SET INDX-1 TO +5 DISPLAY TEST-DAT1(INDX-1)

If you want to process all elements then use PERFORM VARYING INDX-1 FROM +1 BY +1 just like how you would with sub-script variable.
Back to top karnataka
New User

Posted: Fri Oct 06, 2006 11:33 am

Post subject:

IDENTIFICATION DIVISION. PROGRAM-ID. OCCURPGM. DATA DIVISION. WORKING-STORAGE SECTION. 01 ARRAY2. 02 OCCUR2 PIC 9(2) OCCURS 10 TIMES. 01 I PIC 9(2) VALUE 1. 01 VAR2 PIC 9(4) COMP-3. 01 SUB PIC 9(2) VALUE 1. PROCEDURE DIVISION. MAIN-PARA. PERFORM 100-ACCEPT-PARA. PERFORM 200-DISPLAY-PARA. STOP RUN. 100-ACCEPT-PARA. PERFORM UNTIL I = 10 MOVE I TO OCCUR2 ( SUB) ADD 1 TO I ADD 1 TO SUB END-PERFORM.

Joined: 15 Sep 2006 Posts: 20 Location: bangalore

200-DISPLAY-PARA. MOVE 5 TO SUB. DISPLAY ' OCCUR2 ( 5) ===>'OCCUR2 ( 5).


Back to top karnataka
New User

Posted: Fri Oct 06, 2006 11:36 am

Post subject:

hi,

Joined: 15 Sep 2006 Posts: 20 Location: bangalore

the above code is to access 5th element of array using subscript . just check it out wheather it fullfil your request. plz let me know if its wrong........ thanks..

Back to top rajeevdas03 Warnings : 1


New User

Posted: Sun Jan 07, 2007 8:28 pm

Post subject:

Thanks Dave I got it


Joined: 21 Jan 2006 Posts: 9 Location: Mumbai

You might also like