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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SEARCHing multi-level tables

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
I came across this old source I was playing with and never had the time to manufacture an answer until now...so I thought I'd ask about it here...

How do you use SEARCH or SEARCH ALL against a multiple level table? Do these verbs only restrict you to one level or can you successfully search multi-level tables? I never found much of a conclusive answer to this one, so I ended up just rolling my own search and let it be.

The answer I came up with is that SEARCH only seems to allow varying only one index. Like for example, if I vary my first index, I only get hits if the items I search for are in position #1 of the second level. Likewise if I vary the second index I only get to search the first level.

What I ended up doing was throwing SEARCH (varying the second index) into a perform loop that varied the first.

Is there a way to do a complete search on a multi-level table just using SEARCH or SEARCH ALL and not additional code?

A quick simplified table example:
Code:
 01  TEMP-DATA REDEFINES TEMP-FILLER.
     04  TABLE-first OCCURS 12 TIMES INDEXED BY first-NDX.
         08  TABLE-second OCCURS 10 TIMES INDEXED BY second-NDX.
             12  TEMP-VALUE      PIC 9(2).

What my answer ended up being:
Code:
     PERFORM VARYING FIRST-NDX FROM 1 BY 1 UNTIL FIRST-NDX > 12
        SET SECOND-NDX TO 1
        PERFORM 1000-PROC-SECTION
     END-PERFORM.

 1000-PROC-SECTION.
     SEARCH TABLE-SECOND VARYING SECOND-NDX
        AT END DISPLAY 'YOUR VALUE WAS NOT FOUND.'
        WHEN TEMP-VALUE(FIRST-NDX, SECOND-NDX) = INPUT-NUMBER
           DISPLAY 'YOUR VALUE WAS FOUND'
     END-SEARCH.
 
I'd go for this:
Code:
move "false" to sw-found. *> sw-found-true = 88 level 
perform    varying 1st-ndx from 1    by  1 
             until 1st-ndx greater than 12 or sw-found-true
   perform varying 2nd-ndx from 1    by  1 
             until 2nd-ndx greater than 10 or sw-found-true 
      if temp-value (1st-ndx , 2nd-ndx) equal whatever
         move "true" to sw-found
    end-if
  end-perform
end-perform.
 
MFReferenceManual said:
To search an entire two or three dimensional table it is necessarary to execute a SEARCH statement several times. Prior to each execution of a SEARCH statement, SET statements must be executed whenever index-names must be adjusted to appropriate settings.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Glenn9999 said:
Is there a way to do a complete search on a multi-level table just using SEARCH or SEARCH ALL and not additional code?

Assuming you are using a compiler produced in the last two decades, you may nest SEARCH statements. Of course, SEARCH ALL would be appropriate at one or more levels only if the data conformed to the rules for SEARCH ALL at that level(s).

By the way, your example seems contrived (or overly simplified), rather than from a real application, because you have 120 places to store numbers that can have only 100 values. Can you show a more realistic data structure, or the actual problem you are trying to solve? That might permit example code to be on point.

Tom Morrison
 
This will be most efficient. BUT document it well not to leave the maintenance collegue "flabbergasted" in the future.....
Code:
01  TEMP-DATA REDEFINES TEMP-FILLER.
     04  TABLE-first OCCURS 12 TIMES INDEXED BY first-NDX.
         08  TABLE-second OCCURS 10 TIMES INDEXED BY second-NDX.
             12  TEMP-VALUE      PIC 9(2).
01  temp-data-1dim redefines
    temp-data.
    03 temp-data-1dim-elem occurs 120 times indexed by one-dim-idx.
       05 one-dim-value  pic 99.

01  temp-fields                   comp-5.
    03 temp-field1       pic 9(4) value zero.
    03 temp-field2       pic 9(4) value zero.
    03 temp-field3       pic 9(4) value zero.
 
set one-dim-idx to 1.
search temp-data-1dim-elem .....
if found
   set temp-field1 to one-dim-idx
   if temp-field1 greater than 10
      divide 10      into temp-field1
                   giving temp-field2
                remainder temp-field3
      if temp-field3 equal zero
         move 10       to temp-field3
      else 
         add 1         to temp-field2
      end-if
   else
      move 1           to temp-field2
      move temp-field1 to temp-field3 
   end-if 
   set first-ndx       to temp-field2
   set second-ndx      to temp-field3
end-if.
Conversion table to get from a 1-dim address to a 2 dim addrss:

1-dim-addr 2-dim-addr
001 01,01
002 01,02
009 01,09
010 01,10
011 02,01
012 02,02
019 02,09
020 02,10
021 03,01
119 12,09
120 12,10


Ah.. an oppertunity to use the word "flabbergasted". We don't have words like that in dutch :)

Never mind the doucmentation, there will be no COBOL programmers left in the future so who cares :) :)
 
Truusvlugindewind,

And I am still waiting to use uitsmijter, but my wait will be much longer than yours, I think. :-D

Tom Morrison
 
It has been 30 years, but I think I was ordering an open-faced ham & eggs sandwich.

(I didn't get thrown out of any bars, because I didn't want to end up in a canal. :-D)

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top