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!

Table lookup: can one check a range? 1

Status
Not open for further replies.

Dan01

Programmer
Jun 14, 2001
439
US
Hi all, my cobol program will have a prod# and will use a lookup table to assign the correct code to it.

Code Start-prod# End-prod#
001 0001 2222
002 2223 4399
003 4400 6000

If the current record has a value of 3323, how would I assign code value = 002? Will the SEARCH function allow testing a range? For example:
SET X = 1
SEARCH CODE-TABLE
AT END ....
WHEN PROD-NBR >= START-PROD-NO (X) AND
PROD-NBR <= END-PROD-NO (X)
MOVE CODE (X) TO MYCODE
END SEARCH
If not allowed, please show how you might do it. Thanks, Dan.

 
Should work fine with any dialect. The only restriction on the WHEN clause is with SEARCH ALL.
 
Dan,

You have the idea. You want to add an INDEXED BY clause on the group item that contains the OCCURS clause, which in your example is CODE-TABLE:
Code:
01  my-whole-table.
    02  code-table occurs 123 indexed by x.
        03  code     pic x(3).
        03  start-prod-no pic x(4).
        03  end-prod-no pic x(4).


Tom Morrison
 
Dan -

If your table is always in order (ascending in this case), and the ranges don't overlap or have gaps, you can simplify your test somewhat:
Code:
SEARCH CODE-TABLE
  AT END ....
  WHEN END-PROD-NO (X) >= PROD-NBR
      MOVE CODE (X) TO MYCODE
END SEARCH
[\code]

Appropriate comments outlining the assumptions/restrictions should be placed in the code if you go this way.  (And I wouldn't do it this way unless the table is searched a lot as it is not as clear as your current approach.)

One might consider leaving the test the way you have it and shuffling the table entries (with appropriate comments in the code).  One would consider this if there are a lot of entries and some small number are much more likely to occur than the others.  In that case, put the most frequently found ranges first in the table and leave your test as is.

Regards.

Glenn
 
What Glenn said!

Catch a star, Glenn, for good advice!

Tom Morrison
 
If you table is going to have a lot of entries and be search quite a lot, I would personally use a perform varying with a condition to exit early. The search will continue on after the condition has been met and the product code set.

Code:
01  my-whole-table.
    02  X                  pic s9(4).
    02  max-entries        pic s9(4) value +500
    02  code-table occurs 500.
        03  code           pic x(3).
        03  start-prod-no  pic x(4).
        03  end-prod-no    pic x(4).

MOVE SPACES TO MYCODE.
PERFORM VARYING X FROM 1 BY 1 UNTIL 
   MYCODE NOT EQUAL SPACES OR 
   X > MAX-ENTRIES
      if PROD-NBR >= START-PROD-NO (X) AND
         PROD-NBR <= END-PROD-NO (X) 
            MOVE CODE (X) TO MYCODE
      END-IF
END-PERFORM.

IF MYCODE EQUAL ZERO
   not found logic
END-IF.
 
Thanks kkitt. I like the early exit. Dan.
 
Ooooops had a mis-qoute in my previous response:

&quot;The search will continue on after the condition has been met and the product code set.&quot;

Search will stop once the when condition has been met.

The perform varying is primarly used in tables that have varying sizes depending on the data comming in. If the table can have a max of 500 entires but for most runs only 100 are loaded then the early out helps when the entries are not on the table (no need to search empty entries for condition that can not be met). When we load the table we also set a counter that tells how many entries are in the table. So my defintions should have been:

Code:
01  my-whole-table.
    02  X                  pic s9(4).
[RED]
02 num-entries pic s9(4). [/RED]
Code:
    02  max-entries        pic s9(4) value +500
    02  code-table occurs 500.
        03  code           pic x(3).
        03  start-prod-no  pic x(4).
        03  end-prod-no    pic x(4).

MOVE SPACES TO MYCODE.
PERFORM VARYING X FROM 1 BY 1 UNTIL 
   MYCODE NOT EQUAL SPACES OR
[RED]
X > NUM-ENTRIES OR [/RED][/CODE]
X > MAX-ENTRIES
if PROD-NBR >= START-PROD-NO (X) AND
PROD-NBR <= END-PROD-NO (X)
MOVE CODE (X) TO MYCODE
END-IF
END-PERFORM.

IF MYCODE EQUAL ZERO
not found logic
END-IF.
[/CODE]

Sorry for any confusion on the Search Verb.
 
Dan -

You can continue to use the search as you have it coded and still take advantage of the early exit. Just change the OCCURS clause to OCCURS 1 TO 500 TIMES DEPENDING ON NUM-ENTRIES. Of course you may have to change your loading code a bit to ADD 1 TO NUM-ENTRIES BEFORE you move the data to the &quot;new&quot; entry.

Glenn
 
Thanks Glenn. All you guys are great. Thanks for all the good advice. Dan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top