INDEX is set to a high number.

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
cobollearn
Registered Member
Posts: 25
Joined: Tue Jul 09, 2013 6:31 pm

INDEX is set to a high number.

Post by cobollearn »

There is a table in COBOL program which is defined like this:

Code: Select all

05  TAB-REC                     OCCURS  5000  TIMES.
    10  TABLE-NEW               PIC  X(08).         
    10  TABLE-OLD               PIC  X(08).         
    10  TABLE-TYP               PIC  X(03).         
Now later in the program it is used like this:

Code: Select all

3000-GET-MATCHING-JOB.                                
                                                      
    IF  AU-NEW-JOB-NAME       =   TABLE-NEW(INDX)     
        MOVE TABLE-TYP(INDX)  TO  SAVE-AU-JOB-TYP     
        MOVE TABLE-OLD(INDX)  TO  SAVE-AU-OLD-JOB-NAME
        MOVE  'Y'             TO  GOT-JOB-SW          
        MOVE  11000           TO  INDX                
     ELSE                                             
        IF  TABLE-NEW(INDX)   =  SPACES               
            MOVE  6000        TO  INDX.               
My question is on the last line of 3000-paragraph. Why would someone set an INDEX to 6000 while the table OCCURS only 5000 times? Is not it a wrong code?

This is an existing code and I am just analyzing it for some other issue so code is not written by me but is it a common practice to set the Index like this?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1900
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: INDEX is set to a high number.

Post by Robert Sample »

There is probably a sequential SEARCH statement somewhere in the code. With a sequential search, setting the index to a value higher than the table limit will stop the search. This CANNOT stop a binary search, though. From the Enterprise COBOL Version 6.2 Language Reference manual, page 448:
The SEARCH statement executes a serial search beginning at the current setting of the search index.
When the search begins, if the value of the index associated with identifier-1 is not greater than the highest possible occurrence number, the following actions take place:
The conditions in the WHEN phrase are evaluated in the order in which they are written.
If none of the conditions is satisfied, the index for identifier-1 is increased to correspond to the next table element, and step 1 is repeated.
If upon evaluation one of the WHEN conditions is satisfied, the search is terminated immediately, and the imperative-statement-2 associated with that condition is executed. The index points to the table element that satisfied the condition. If NEXT SENTENCE is specified, control passes to the statement following the closest period.
If the end of the table is reached (that is, the value of the incremented index is greater than the highest possible occurrence number) without the WHEN condition being satisfied, the search is terminated.
While the code is not wrong, per se, it also is somewhat misleading -- there should have been some search termination variable and logic implemented in the program rather than just setting the table index higher than the table limit. As you have seen, using this termination method requires additional understanding. It also can lead to subtle bugs -- what happens if the table limit is increased to 6000 or 8000 due to changing circumstances?
cobollearn
Registered Member
Posts: 25
Joined: Tue Jul 09, 2013 6:31 pm

Re: INDEX is set to a high number.

Post by cobollearn »

Thanks Robert.

You are correct, there is a sequential search in the program. I had to change that search to BINARY search and that did not work with this code, as you said. This code was needed to be changed. Thanks again for your explanation.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”