IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to define and access an array in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rajeevdas03
Warnings : 1

New User


Joined: 21 Jan 2006
Posts: 19
Location: Mumbai

PostPosted: Wed Oct 04, 2006 11:02 am
Reply with quote

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
View user's profile Send private message
DavidatK

Active Member


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

PostPosted: Wed Oct 04, 2006 10:35 pm
Reply with quote

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
View user's profile Send private message
MFRASHEED

Active User


Joined: 14 Jun 2005
Posts: 186
Location: USA

PostPosted: Wed Oct 04, 2006 11:27 pm
Reply with quote

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
View user's profile Send private message
karnataka

New User


Joined: 15 Sep 2006
Posts: 20
Location: bangalore

PostPosted: Fri Oct 06, 2006 11:33 am
Reply with quote

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.

200-DISPLAY-PARA.
MOVE 5 TO SUB.
DISPLAY ' OCCUR2 ( 5) ===>'OCCUR2 ( 5).
Back to top
View user's profile Send private message
karnataka

New User


Joined: 15 Sep 2006
Posts: 20
Location: bangalore

PostPosted: Fri Oct 06, 2006 11:36 am
Reply with quote

hi,


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
View user's profile Send private message
rajeevdas03
Warnings : 1

New User


Joined: 21 Jan 2006
Posts: 19
Location: Mumbai

PostPosted: Sun Jan 07, 2007 8:28 pm
Reply with quote

Thanks Dave I got it
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Access to non cataloged VSAM file JCL & VSAM 18
No new posts How to access web services/website? Mainframe Interview Questions 4
Search our Forums:

Back to Top