|
1 | 1 | ******************************************************************
|
2 | 2 | * author: Erik Eriksen
|
3 | 3 | * date: 2021-08-30
|
4 |
| - * purpose: Example using the search syntax. |
| 4 | + * updated: 2022-04-28 |
| 5 | + * purpose: Example using the search and search all syntax. |
5 | 6 | * tectonics: cobc
|
6 | 7 | ******************************************************************
|
7 | 8 | identification division.
|
|
10 | 11 | file section.
|
11 | 12 | working-storage section.
|
12 | 13 |
|
13 |
| - *> Table must have asc or desc indexed key for binary searching |
| 14 | + *> Table must have asc or desc indexed key for binary ("all") searching |
| 15 | + *> Note: Does not need multiple keys. Multiple keys are just used |
| 16 | + *> here to demonstrate that you can have them. |
14 | 17 | 01 ws-item-table occurs 3 times
|
15 | 18 | ascending key is
|
16 | 19 | ws-item-id-1, ws-item-id-2
|
|
28 | 31 | 10 filler pic x value "/".
|
29 | 32 | 10 ws-item-day pic 99.
|
30 | 33 |
|
| 34 | + |
| 35 | + *> Sequential searching does not require a key or the data to |
| 36 | + *> be sorted in the table. (But is slower) |
| 37 | + 01 ws-no-key-item-table occurs 3 times indexed by idx-2. |
| 38 | + 05 ws-no-key-id pic 9(4). |
| 39 | + 05 ws-no-key-value pic x(25). |
| 40 | + |
| 41 | + |
31 | 42 | 01 ws-accept-id-1 pic 9(4).
|
32 | 43 | 01 ws-accept-id-2 pic 9(4).
|
33 | 44 | 01 ws-accept-id-3 pic 9(4).
|
|
36 | 47 | main-procedure.
|
37 | 48 | perform setup-test-data
|
38 | 49 |
|
39 |
| - |
| 50 | + display space |
| 51 | + display "==================================================" |
| 52 | + display "Searching keyed table using binary search." |
40 | 53 | display "Enter id-1 to search for: " with no advancing
|
41 | 54 | accept ws-accept-id-1
|
42 | 55 |
|
43 | 56 | *> Binary search - table must be indexed by an asc or desc id
|
44 | 57 | *> and sorted for search to work. MUCH faster than sequential
|
45 | 58 | *> search which does not require any sorting or indexing.
|
46 | 59 | *> Binary search is indicated by the "SEARCH ALL" syntax.
|
| 60 | + set idx to 1 |
47 | 61 | search all ws-item-table
|
48 | 62 | at end
|
49 | 63 | display "Item not found."
|
50 | 64 | when ws-item-id-1(idx) = ws-accept-id-1
|
51 | 65 | perform display-found-item
|
52 | 66 | end-search
|
53 | 67 |
|
54 |
| - |
| 68 | + display space |
| 69 | + display "==================================================" |
55 | 70 | display "Searching again with all required ids matching."
|
56 | 71 |
|
57 | 72 | display "Enter id-1 to search for: " with no advancing
|
|
63 | 78 | display "Enter id-3 to search for: " with no advancing
|
64 | 79 | accept ws-accept-id-3
|
65 | 80 |
|
| 81 | + set idx to 1 |
66 | 82 | search all ws-item-table
|
67 | 83 | at end
|
68 | 84 | display "Item not found."
|
|
72 | 88 | perform display-found-item
|
73 | 89 | end-search
|
74 | 90 |
|
| 91 | + *> Sequential searches are slower but also don't require the data |
| 92 | + *> to be sorted or require a key. |
| 93 | + display space |
| 94 | + display "==================================================" |
| 95 | + display "Searching not keyed table using sequential search." |
| 96 | + display "Enter id: " with no advancing |
| 97 | + accept ws-accept-id-1 |
| 98 | + |
| 99 | + set idx-2 to 1 |
| 100 | + search ws-no-key-item-table |
| 101 | + at end |
| 102 | + display "Item not found." |
| 103 | + when ws-no-key-id(idx-2) = ws-accept-id-1 |
| 104 | + display " Record found:" |
| 105 | + display "---------------" |
| 106 | + display " ws-no-key-id: " ws-no-key-id(idx-2) |
| 107 | + display "ws-no-key-value: " ws-no-key-value(idx-2) |
| 108 | + display space |
| 109 | + end-search |
| 110 | + |
| 111 | + display space |
75 | 112 |
|
76 | 113 | stop run.
|
77 | 114 |
|
|
83 | 120 | display "Item id-3: " ws-item-id-3(idx)
|
84 | 121 | display "Item Name: " ws-item-name(idx)
|
85 | 122 | display "Item Date: " ws-item-date(idx)
|
86 |
| - |
| 123 | + display space |
87 | 124 | exit paragraph.
|
88 | 125 |
|
89 | 126 |
|
|
107 | 144 | move "test item 3" to ws-item-name(3)
|
108 | 145 | move "2021/03/03" to ws-item-date(3)
|
109 | 146 |
|
| 147 | + move 2 to ws-no-key-id(1) |
| 148 | + move "Value of id 2." to ws-no-key-value(1) |
| 149 | + |
| 150 | + move 3 to ws-no-key-id(2) |
| 151 | + move "Value of id 3." to ws-no-key-value(2) |
| 152 | + |
| 153 | + move 1 to ws-no-key-id(3) |
| 154 | + move "Value of id 1." to ws-no-key-value(3) |
110 | 155 |
|
111 | 156 | exit paragraph.
|
112 | 157 |
|
|
0 commit comments