I would like informations to manipulate tables.
I encounter few problem with a piece of cobol code like below:
01 TABLE-1.
05 STRUCT-1 OCCURS 25 TIMES.
10 VALUE-1 PIC AAA.
10 VALUE-2 PIC 9(5)V999.
05 NUMBER-OF-OCCURS PIC 99.
How do you update values? (update a VALUE-2 when you know a VALUE-1)
How look up a value and add new one?
Thanks a lot!
How to look up a value/How to update a value
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 key value. COBOL provides a couple of ways to do this. I recommend that you start by
reviewing the COBOL SEARCH
statement. If
STRUCT-1records are sorted, you could useSEARCH ALL, otherwise you must useSEARCHor just codeyour 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
STRUCT-1table. COBOL provides theINDEXED BYphrase on theOCCURSclause to declare an index specific to the given table(see OCCURS)
Once you have set the index into
STRUCT-1to point to the row to be updated you justMOVEthevalue to the appropriate variables within that row, for example
MOVE 123.456 TO VALUE-2 (IDX-1)
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.
How to add a new row
First recognize that
STRUCT-1contains exactly 25 rows. COBOL does not have a mechanism to dynamicallyincrease 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
NUMBER-OF-OCCURSdoes this job(I didn’t mention it but, you need this variable to bound the SEARCH discussed above).
To ‘add’ a row, just increment
NUMBER-OF-OCCURSby 1. Be careful not to exceed the table size. Examplecode might be:
The above code avoids the explicit use of the number of occurs in
TABLE-1which in turn can savea number of maintenance problems when/if the number of OCCURS is ever changed.
See the NOTE at the bottom: There is a really big Woops here – did you catch it!
Now back to the search problem. The following code example illustrates how you might
proceed:
WORKING-STORAGE Declaration:
What was added:
FOUND-INDis used to indicate whether the row you are looking for has been found. The 88 levels give specific values to set/testMAX-IDXis used to set an upper bound limit on the search. You could useNUMBER-OF-OCCURSin the upper bounds test but this would force a data type converson on every test which isn’t very efficientIDX-1is used as the index (offset) into theSTRUCT-1table.Personally, I would declare
NUMBER-OF-OCCURSasPIC S9(4) BINARYbut what you have will work.Assuming that
STRUCT-1is not sorted andNUMBER-OF-OCCURSrepresents the currentnumber of active rows in
STRUCT-1thisis an example of how you might code the
SEARCHwhen looking for the value ‘ABC’:How it works:
FOUND-NOto true.IFensures that there is at least 1 active row inSTRUCT-1before beginning the search (it is an error to set an INDEX to zero – so you need to guard against that).SEARCHterminates when the firstSEARCH WHENclause is satisified. That is why the ‘do nothing’ verbCONTINUEcan 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 whereFOUND-YEScan be set.SEARCHcompletes, test for success or failure then act accordingly.Some exercises for you to research:
AT ENDclause in theSEARCHstatement?VARYINGclause in theSEARCHstatement?WHEREclauses in the order that I did?Hope this gets you started down the right path.
Edit
In response to your question in the comments: Could we use NUMBER-OF-OCCURS as index for the search. The
answer is yes, but you need to implement some different rules. When using
NUMBER-OF-OCCURSas 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
STRUCT-1.This might be accomplished by initializing un-used rows with a sentinal value (eg.
LOW-VALUE) that youwill never actually want to put into the table. The
SEARCHbecomes:The above will search every row in
STRUCT-1in the event that the value you are searching for(ie.
ABC) is not in the table. As an optimization you can add a secondWHENclause to terminate thesearch upon finding a sentinal value:
The above assumes
LOW-VALUEwas used to identify unused rows. You can also dropIDX-1andMAX-IDXfrom your working storage since this solution doesn’t need them.
Using
NUMBER-OF-OCCURSas an index also means you must change the way you search for an empty rowto insert a new value. The easiest way to do this is to search the table using the above
code for
LOW-VALUEinstead of'ABC'. IfFOUND-YEShas been set at the end of the search, thenNUMBER-OF-OCCURSis the index of the first unused row. IfFOUND-NOhas been set, then the table isalready full.
The above code is a lot simpler than what I initially suggested. So why did I give you the more
complicated solution? The more complicated 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
SEARCHto find the next unused row. These efficienciesmay 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
PIC 99field into an index reference).Note:
My original example to calculate whether the table was full using the
LENGTH OFspecial registerwould work in this example but has a really bad built in assumption! The
LENGTH OF TABLE-1includesnot only the
STRUCT-1table but theNUMBER-OF-OCCURStoo. The length ofNUMBER-OF-OCCURSis less than oneoccurance of
STRUCT-1so 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:
and the bounds calculation would become:
Or you could just move
NUMBER-OF-OCCURSout of theTABLE-1record definition.