Skip to content

Commit d9295bf

Browse files
committed
Updated search example and added a readme
1 parent be75a11 commit d9295bf

File tree

2 files changed

+116
-5
lines changed

2 files changed

+116
-5
lines changed

search/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# SEARCH and SEARCH ALL Example
2+
3+
4+
```SEARCH``` and ```SEARCH ALL``` are used to search an indexed table array for specified matching condition(s). There are two
5+
types of search. There's a binary search (```SEARCH ALL```) and a sequential search (```SEARCH```). Binary
6+
search requires that the data is sorted and has a ascending or descending key (can have multiple keys as well).
7+
A sequential search does not have a key nor does it require that the data is presorted. Note: Sequential searches
8+
are slower than binary searchs.
9+
10+
11+
**Basic example of syntax**
12+
13+
```
14+
set idx to 1
15+
search ws-item-table
16+
at end
17+
display "Item not found."
18+
when ws-item-id-1(idx) = ws-accept-id-1
19+
display "Item found."
20+
end-search
21+
```
22+
23+
24+
```search.cbl``` demonstrates a couple of examples of using ```SEARCH``` and ```SEARCH ALL``` in different forms.
25+
26+
27+
**Example of program output:**
28+
29+
```
30+
31+
==================================================
32+
Searching keyed table using binary search.
33+
Enter id-1 to search for: 3
34+
Record found:
35+
----------------
36+
Item id-1: 0003
37+
Item id-2: 0103
38+
Item id-3: 0498
39+
Item Name: test item 3
40+
Item Date: 2021/03/03
41+
42+
43+
==================================================
44+
Searching again with all required ids matching.
45+
Enter id-1 to search for: 2
46+
Enter id-2 to search for: 102
47+
Enter id-3 to search for: 499
48+
Record found:
49+
----------------
50+
Item id-1: 0002
51+
Item id-2: 0102
52+
Item id-3: 0499
53+
Item Name: test item 2
54+
Item Date: 2021/02/02
55+
56+
57+
==================================================
58+
Searching not keyed table using sequential search.
59+
Enter id: 1
60+
Record found:
61+
---------------
62+
ws-no-key-id: 0001
63+
ws-no-key-value: Value of id 1.
64+
65+
```
66+

search/search_example.cbl search/search.cbl

+50-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
******************************************************************
22
* author: Erik Eriksen
33
* 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.
56
* tectonics: cobc
67
******************************************************************
78
identification division.
@@ -10,7 +11,9 @@
1011
file section.
1112
working-storage section.
1213

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.
1417
01 ws-item-table occurs 3 times
1518
ascending key is
1619
ws-item-id-1, ws-item-id-2
@@ -28,6 +31,14 @@
2831
10 filler pic x value "/".
2932
10 ws-item-day pic 99.
3033

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+
3142
01 ws-accept-id-1 pic 9(4).
3243
01 ws-accept-id-2 pic 9(4).
3344
01 ws-accept-id-3 pic 9(4).
@@ -36,22 +47,26 @@
3647
main-procedure.
3748
perform setup-test-data
3849

39-
50+
display space
51+
display "=================================================="
52+
display "Searching keyed table using binary search."
4053
display "Enter id-1 to search for: " with no advancing
4154
accept ws-accept-id-1
4255

4356
*> Binary search - table must be indexed by an asc or desc id
4457
*> and sorted for search to work. MUCH faster than sequential
4558
*> search which does not require any sorting or indexing.
4659
*> Binary search is indicated by the "SEARCH ALL" syntax.
60+
set idx to 1
4761
search all ws-item-table
4862
at end
4963
display "Item not found."
5064
when ws-item-id-1(idx) = ws-accept-id-1
5165
perform display-found-item
5266
end-search
5367

54-
68+
display space
69+
display "=================================================="
5570
display "Searching again with all required ids matching."
5671

5772
display "Enter id-1 to search for: " with no advancing
@@ -63,6 +78,7 @@
6378
display "Enter id-3 to search for: " with no advancing
6479
accept ws-accept-id-3
6580

81+
set idx to 1
6682
search all ws-item-table
6783
at end
6884
display "Item not found."
@@ -72,6 +88,27 @@
7288
perform display-found-item
7389
end-search
7490

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
75112

76113
stop run.
77114

@@ -83,7 +120,7 @@
83120
display "Item id-3: " ws-item-id-3(idx)
84121
display "Item Name: " ws-item-name(idx)
85122
display "Item Date: " ws-item-date(idx)
86-
123+
display space
87124
exit paragraph.
88125

89126

@@ -107,6 +144,14 @@
107144
move "test item 3" to ws-item-name(3)
108145
move "2021/03/03" to ws-item-date(3)
109146

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)
110155

111156
exit paragraph.
112157

0 commit comments

Comments
 (0)