Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>How to look up a value/How to update a value</strong></p> <p>First you have to look up the record (row) that you want to update. This is typically done by searching the table for a given <em>key</em> value. COBOL provides a couple of ways to do this. I recommend that you start by reviewing the COBOL <a href="http://publibfp.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/IGY3LR31/6.2.32?DT=20060329140556" rel="noreferrer">SEARCH</a> statement. If <code>STRUCT-1</code> records are sorted, you could use <code>SEARCH ALL</code>, otherwise you must use <code>SEARCH</code> or just code your own search loop. In order to use any of these techniques you will need to declare another variable somewhere in your program to use as an index (offset) into the <code>STRUCT-1</code> table. COBOL provides the <code>INDEXED BY</code> phrase on the <code>OCCURS</code> clause to declare an index specific to the given table (see <a href="http://publibfp.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/IGY3LR31/5.3.11?DT=20060329140556" rel="noreferrer">OCCURS</a>)</p> <p>Once you have set the index into <code>STRUCT-1</code> to point to the row to be updated you just <code>MOVE</code> the value to the appropriate variables within that row, for example</p> <p>MOVE 123.456 TO VALUE-2 (IDX-1)</p> <p>where IDX-1 is the index referred to above. Note that you can use either an integer or index variable to specify the row number to be updated, you are not limited to using an INDEX type variable. However, it is generally more efficient to use INDEX variables over other types of variables, particularily when working with multi-dimensional tables where the program makes lots of references to the table.</p> <p><strong>How to add a new row</strong></p> <p>First recognize that <code>STRUCT-1</code> contains exactly 25 rows. COBOL does not have a mechanism to dynamically increase or decrease this number (I've heard this will possible in the next ISO COBOL standard - but don't hold your breath waiting for it). Technically all 25 rows are available at any time. However a common convention is to 'grow' a table from being empty to full sequentially, one row at a time. To use this convention you need to assign a variable to keep track of the last used row number (don't forget to initialize this variable to zero at program startup). In your example it looks like the variable <code>NUMBER-OF-OCCURS</code> does this job (I didn't mention it but, you need this variable to bound the SEARCH discussed above).</p> <p>To 'add' a row, just increment <code>NUMBER-OF-OCCURS</code> by 1. Be careful not to exceed the table size. Example code might be:</p> <pre><code>IF NUMBER-OF-OCCURS &lt; (LENGTH OF TABLE-1 / LENGTH OF STRUCT-1 (1)) ADD +1 TO NUMBER-OF-OCCURS ELSE table is full, preform some error/recovery routine END-IF </code></pre> <p>The above code avoids the explicit use of the number of occurs in <code>TABLE-1</code> which in turn can save a number of maintenance problems when/if the number of OCCURS is ever changed.</p> <p><strong>See the NOTE at the bottom: There is a really big Woops here - did you catch it!</strong></p> <p>Now back to the search problem. The following code example illustrates how you might proceed:</p> <p>WORKING-STORAGE Declaration:</p> <pre><code> 01 FOUND-IND PIC X(1). 88 FOUND-YES VALUE 'Y'. 88 FOUND-NO VALUE 'N'. 77 MAX-IDX USAGE IS INDEX. 01 TABLE-1. 05 STRUCT-1 OCCURS 25 TIMES INDEXED BY IDX-1. 10 VALUE-1 PIC AAA. 10 VALUE-2 PIC 9(5)V999. 05 NUMBER-OF-OCCURS PIC 99. </code></pre> <p>What was added:</p> <ul> <li><code>FOUND-IND</code> is used to indicate whether the row you are looking for has been found. The 88 levels give specific values to set/test</li> <li><code>MAX-IDX</code> is used to set an upper bound limit on the search. You could use <code>NUMBER-OF-OCCURS</code> in the upper bounds test but this would force a data type converson on every test which isn't very efficient </li> <li><code>IDX-1</code> is used as the index (offset) into the <code>STRUCT-1</code> table.</li> </ul> <p>Personally, I would declare <code>NUMBER-OF-OCCURS</code> as <code>PIC S9(4) BINARY</code> but what you have will work.</p> <p>Assuming that <code>STRUCT-1</code> is not sorted and <code>NUMBER-OF-OCCURS</code> represents the current number of active rows in <code>STRUCT-1</code> this is an example of how you might code the <code>SEARCH</code> when looking for the value 'ABC':</p> <pre><code>SET FOUND-NO TO TRUE IF NUMBER-OF-OCCURS &gt; ZERO SET IDX-1 TO 1 SET MAX-IDX TO NUMBER-OF-OCCURS SEARCH STRUCT-1 WHEN IDX-1 &gt; MAX-IDX CONTINUE WHEN VALUE-1 (IDX-1) = 'ABC' SET FOUND-YES TO TRUE END-SEARCH END-IF IF FOUND-YES row found, use IDX-1 to reference the row containing 'ABC' ELSE row not found, IDX-1 does not contain a valid index END-IF </code></pre> <p>How it works:</p> <ul> <li>Start by assuming the row is not in the table by setting <code>FOUND-NO</code> to true.</li> <li>The first <code>IF</code> ensures that there is at least 1 active row in <code>STRUCT-1</code> before beginning the search (it is an error to set an INDEX to zero - so you need to guard against that).</li> <li>The <code>SEARCH</code> terminates when the first <code>SEARCH WHEN</code> clause is satisified. That is why the 'do nothing' verb <code>CONTINUE</code> can be used when we run out of rows to search. The other terminating condition (finding the value you are looking for) is the only place where <code>FOUND-YES</code> can be set.</li> <li>When the <code>SEARCH</code> completes, test for success or failure then act accordingly.</li> </ul> <p>Some exercises for you to research:</p> <ul> <li>Why did I not have to code an <code>AT END</code> clause in the <code>SEARCH</code> statement?</li> <li>Why did I not have to code a <code>VARYING</code> clause in the <code>SEARCH</code> statement?</li> <li>Why did I code the <code>WHERE</code> clauses in the order that I did?</li> </ul> <p>Hope this gets you started down the right path.</p> <p><strong>Edit</strong></p> <p>In response to your question in the comments: <em>Could we use NUMBER-OF-OCCURS as index for the search</em>. The answer is yes, but you need to implement some different rules. When using <code>NUMBER-OF-OCCURS</code> as an index you can no longer use it to keep track of how many rows currently contain valid data. This means you need another mechanism to identify unused rows in <code>STRUCT-1</code>. This might be accomplished by initializing un-used rows with a sentinal value (eg. <code>LOW-VALUE</code>) that you will never actually want to put into the table. The <code>SEARCH</code> becomes:</p> <pre><code>SET FOUND-NO TO TRUE MOVE 1 TO NUMBER-OF-OCCURS SEARCH STRUCT-1 VARYING NUMBER-OF-OCCURS WHEN VALUE-1 (NUMBER-OF-OCCURS) = 'ABC' SET FOUND-YES TO TRUE END-SEARCH </code></pre> <p>The above will search every row in <code>STRUCT-1</code> in the event that the value you are searching for (ie. <code>ABC</code>) is not in the table. As an optimization you can add a second <code>WHEN</code> clause to terminate the search upon finding a sentinal value:</p> <pre><code>WHEN VALUE-1 (NUMBER-OF-OCCURS) = LOW-VALUE CONTINUE </code></pre> <p>The above assumes <code>LOW-VALUE</code> was used to identify unused rows. You can also drop <code>IDX-1</code> and <code>MAX-IDX</code> from your working storage since this solution doesn't need them. </p> <p>Using <code>NUMBER-OF-OCCURS</code> as an index also means you must change the way you search for an empty row to insert a new value. The easiest way to do this is to search the table using the above code for <code>LOW-VALUE</code> instead of <code>'ABC'</code>. If <code>FOUND-YES</code> has been set at the end of the search, then <code>NUMBER-OF-OCCURS</code> is the index of the first unused row. If <code>FOUND-NO</code> has been set, then the table is already full.</p> <p>The above code is a lot simpler than what I initially suggested. So why did I give you the more <em>complicated</em> solution? The more <em>complicated</em> solution is more efficient because it makes many fewer internal offset calculations and data type conversions when running through the table. It also avoids doing an additional <code>SEARCH</code> to find the next unused row. These efficiencies may not be of concern in your application, but if the tables are large and accessed frequently you should be aware of the performance aspect of searching tables and forced data type conversions (for example the cost of converting a <code>PIC 99</code> field into an index reference). </p> <p><strong>Note:</strong></p> <p>My original example to calculate whether the table was full using the <code>LENGTH OF</code> special register would work in this example but has a really <strong>bad</strong> built in assumption! The <code>LENGTH OF TABLE-1</code> includes not only the <code>STRUCT-1</code> table but the <code>NUMBER-OF-OCCURS</code> too. The length of <code>NUMBER-OF-OCCURS</code> is less than one occurance of <code>STRUCT-1</code> so it all works out ok due to truncation of the result into an integer value. This is a very good example of code working correctly for the wrong reason! To make the proper calculation you would have to adjust your working storage to something like:</p> <pre><code>01 TABLE-1. 05 STRUCT-TABLE. 10 STRUCT-1 OCCURS 25 TIMES. 20 VALUE-1 PIC AAA. 20 VALUE-2 PIC 9(5)V999. 05 NUMBER-OF-OCCURS PIC 99. </code></pre> <p>and the bounds calculation would become:</p> <pre><code>IF NUMBER-OF-OCCURS &lt; (LENGTH OF STRUCT-TABLE / LENGTH OF STRUCT-1 (1)) ADD +1 TO NUMBER-OF-OCCURS ELSE table is full, preform some error/recovery routine END-IF </code></pre> <p>Or you could just move <code>NUMBER-OF-OCCURS</code> out of the <code>TABLE-1</code> record definition.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload