Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search All

Status
Not open for further replies.

npolito

MIS
Feb 6, 2003
2
US
The table is loading fine. But when I go to search the table it only finds the same one record . Like the index is not incrementing. Any help or hints you can give me would be appreciated.
Here is the relevant code below.


WORKING-STORAGE SECTION.

01 WORKING-VARIABLES.
05 TABLE-ROW-POINTER PIC S9(4) BINARY.
05 TABLE-MAX-COUNTER PIC 99 VALUE ZERO.

01 DISHNETW-TABLE.
05 DISHNETW-TABLE-T OCCURS 1 TO 40 TIMES
DEPENDING ON TABLE-MAX-COUNTER
DESCENDING KEY IS NETWORK-ID
INDEXED BY NETW-INDEX.
10 NETWORK-ID PIC X(8).
10 PPV PIC X.

100-INITIALIZE SECTION.
*=================================================================
OPEN INPUT DISHTRAN-FILE
DISHNETW-FILE
DISHEVNT-FILE
OUTPUT REPORT-FILE.
PERFORM 120-READ-NETW VARYING NETW-INDEX FROM 1 BY 1
UNTIL EOF-NETW = 'Y'.
CLOSE DISHNETW-FILE.

120-READ-NETW SECTION.
*=================================================================
READ DISHNETW-FILE INTO DISHNETW-TABLE-T (NETW-INDEX)
AT END MOVE 'Y' TO EOF-NETW
NOT AT END ADD 1 TO TABLE-MAX-COUNTER
DISPLAY 'DISHNETW-TABLE-T : ' DISHNETW-TABLE-T (NETW-INDEX)
IF DISHNETW-TABLE-T (NETW-INDEX) = SPACES
MOVE 'Y' TO EOF-NETW
END-IF.

120-EXIT.
EXIT.

IF NETWORK-B ALPHABETIC
MOVE 1 TO TABLE-ROW-POINTER
SEARCH ALL DISHNETW-TABLE-T
AT END MOVE 6 TO E
PERFORM 300-ERROR
SUBTRACT 1 FROM TABLE-ROW-POINTER
DISPLAY 'NETWORK-ID TRP : ' NETWORK-ID (TABLE-ROW-POINTER)
DISPLAY 'NETWORK-ID : ' NETWORK-ID (NETW-INDEX)
WHEN NETWORK-ID (NETW-INDEX) = NETWORK-IN
CONTINUE
END-SEARCH
ELSE MOVE 4 TO E
PERFORM 300-ERROR
END-IF.

 
I am not familiar with this table syntax but it looks like your at end condition falls thru to the display.
 
Are you positive your records are read in in descending order by NETWORK-ID? If not, you'll need to sort the table that way BEFORE you can use SEARCH ALL.

A couple of other comments. Your procedure to load the table contains a technical flaw in that you move the record to the next entry in the table before incrementing the maximum table size. You should do the increment before moving the data. You should also check for more than 40 records input:
Code:
PERFORM VARYING NETW-INDEX FROM 1 BY 1
   UNTIL EOF-NETW = 'Y'
   READ         
      AT END 
          MOVE 'Y' TO EOF-NETW
      NOT AT END 
          ADD 1 TO TABLE-MAX-COUNTER
          IF TABLE-MAX-COUNTER > 40
              MOVE 7           TO E
              PERFORM 300-ERROR
          END-IF
          MOVE DISHNETW-RECORD TO 
                      DISHNETW-TABLE-T (NETW-INDEX)
    END-READ
END-PERFORM
[\CODE]
 
OK, I see the biggest issue that I was wrong about. The data is not sorted. So I guess I should be using a search command instead of search all. I am doing this for class and we have not got to the sort command yet. I will have to try and rework this and see if the search command works.
 
You will find that the sort verb is not particularly useful for this. It is good for sorting records in a file but not a table. There has been talk of COBOL providing a method of sorting tables internally, but I don't know where that stands. With a small table, doing a simple bubble sort can make the table usable by SEARCH ALL. SEARCH ALL should be used for frequently accessed tables when they get to be a dozen or so elements long (my rule of thumb).

Just a warning on the plain SEARCH: make sure you start your index at 1 before the SEARCH verb as it doesn't initialize the index like SEARCH ALL does.

Glenn
 
For a small table like this, a normal sequential search would likely be best, unless you are searching it a LOT of times. Then do
SORT DISHNETW-TABLE-T
after the table is loaded. That will sort it into the proper sequence for SEARCH ALL. The table definition parameters that make SEARCH ALL work will be the default for SORT.
 
The SORT table statement is available in the new 2002 COBOL standard. I'm not sure how many compilers implement that standard.

A sequential search of a 40 element table (assuming random entries and lookups) takes, on average, 20 compares to locate an entry. If the table is sorted, a binary search (SEARCH ALL) takes only about 6 compares. So, as I said, if the table is searched frequently and is longer than about a dozen entries, sort it and use SEARCH ALL.

Regards.

Glenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top