View previous topic :: View next topic
|
Author |
Message |
jaguyria
New User
Joined: 15 Feb 2022 Posts: 21 Location: Portugal
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
jaguyria
New User
Joined: 15 Feb 2022 Posts: 21 Location: Portugal
|
|
|
|
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 |
|
Back to top |
|
|
|