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

Search substring in internal table of a cobol program


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

New User


Joined: 15 Feb 2022
Posts: 21
Location: Portugal

PostPosted: Thu Apr 07, 2022 10:38 pm
Reply with quote

Hello,

I have a doubt in the search of internal table of a program cobol.
Sorry if there's something already related with that in this forum, but from my recherche I haven't found nothing yet.

I have a table defined like:
Code:

01 W-FIND-CODE     PIC X(05).
01 W-CODE-FOUND    PIC X(10).
*----------------- TABLE CARS                     
 01 TB-CARS                     VALUE   SPACES.           
    05 LG-CARS     OCCURS   1   TO  30                   
                      DEPENDING ON CPT-CARS               
                      ASCENDING KEY IS T-CARS             
                      INDEXED BY IX-CARS.                 
       07 TAB-CARS.                                       
          10 T-CARS.                                     
             15 T-CARS-DEB      PIC X(03).               
             15 T-CARS-CODE     PIC X(05).               
             15 T-CARS-REST     PIC X(09).               
          10 T-CARS-LIB.                                 
             15 T-CARS-POST.                             
                20 T-CARS-CODE2  PIC X(10).               
                20 T-CARS-FILLER PIC X(56).               
             15 T-CARS-LIB2      PIC X(221).   


I want to search the code W-FIND-CODE in the table TB-CARS , regarding the variable T-CARS-CODE.

I get always an error in the 2 tries that I made:

1st try:
Code:
SEARCH-CARS.
*----------*                                                         
     SEARCH ALL                 LG-CARS AT END                   
          MOVE '1' TO   W-IND-RDJ                                   
     WHEN T-CARS(IX-CARS)(4:5) = W-FIND-CODE                                                           
"T- CARS" was reference modified and reference modification is not
allowed in this context.  The statement was discarded.
          MOVE '0' TO   W-IND-RDJ                                   
     END-SEARCH                                                     
     IF W-IND-RDJ = '0'                                             
         MOVE T-CARS-CODE2(IX-CARS)   TO   W-CODE-FOUND               
     ELSE                                                           
         MOVE SPACES                       TO W-CODE-FOUND               
     END-IF                                                         
     .                                                               


2nd try:
Code:
SEARCH-CARS.
*----------*                                                         
     SEARCH ALL                 LG-CARS AT END                   
          MOVE '1' TO   W-IND-RDJ                                   
     WHEN T-CARS-CODE(IX-KVNM192) = W-FIND-CODE                                                           
The left side operand, "T-KVNM192-CODE (ALPHANUMERIC)", of a "WHEN"
phrase of a "SEARCH ALL" statement was not a key of the table being
searched.  The statement was discarded.                   
          MOVE '0' TO   W-IND-RDJ                                   
     END-SEARCH                                                     
     IF W-IND-RDJ = '0'                                             
         MOVE T-CARS-CODE2(IX-CARS)   TO W-CODE-FOUND               
     ELSE                                                           
         MOVE SPACES                       TO   W-CODE-FOUND               
     END-IF                                                         
     .                                   


Does anyone can see a solution for my problem?

Thanks in advance!
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Apr 08, 2022 12:04 am
Reply with quote

I haven't tested this code, but try:
Code:
01 W-FIND-CODE     PIC X(05).
01 W-CODE-FOUND    PIC X(10).
*----------------- TABLE CARS                     
 01 TB-CARS                     VALUE   SPACES.           
    05 LG-CARS     OCCURS   1   TO  30                   
                      DEPENDING ON CPT-CARS               
                      ASCENDING KEY IS T-CARS             
                                       T-CARS-CODE
                      INDEXED BY IX-CARS.                 
       07 TAB-CARS.                                       
          10 T-CARS.                                     
             15 T-CARS-DEB      PIC X(03).               
             15 T-CARS-CODE     PIC X(05).               
             15 T-CARS-REST     PIC X(09).               
          10 T-CARS-LIB.                                 
             15 T-CARS-POST.                             
                20 T-CARS-CODE2  PIC X(10).               
                20 T-CARS-FILLER PIC X(56).               
             15 T-CARS-LIB2      PIC X(221). 
and use
Code:
     SEARCH ALL                 LG-CARS AT END                   
          MOVE '1' TO   W-IND-RDJ                                   
     WHEN T-CARS-CODE(IX-KVNM192) = W-FIND-CODE
The variable in the WHEN clause MUST be an ASCENDING (or DESCENDING) key of the table.

You may have to make some adjustments since T-CARS and T-CARS-CODE overlap. Also note that SEARCH ALL cannot be used for T-CARS-CODE since you will get unpredictable results; the only way for SEARCH ALL to work correctly is against the primary key of the table (that is, the first variable after the ASCENDING KEY clause).
Back to top
View user's profile Send private message
jaguyria

New User


Joined: 15 Feb 2022
Posts: 21
Location: Portugal

PostPosted: Sun Apr 10, 2022 7:54 am
Reply with quote

Thank you Robert Sample, it was very useful.

I've decided to declare internal table as:

Code:
 01 TB-CARS                     VALUE   SPACES.           
    05 LG-CARS     OCCURS   1   TO  30                   
                      DEPENDING ON CPT-CARS               
                      ASCENDING KEY IS T-CARS-CODE
                      INDEXED BY IX-CARS. 


Thank you again icon_smile.gif
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 2
No new posts Using API Gateway from CICS program CICS 0
No new posts Load new table with Old unload - DB2 DB2 6
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts Search two or more word with FILEAID Compuware & Other Tools 15
Search our Forums:

Back to Top