A defined structure can appear in the FILE SECTION as a RECORD as well.
A record means a data structure in a graph, starting with an upper level as the root and continues with lower-level elements (level numbers in classic COBOL: 01-05-10-..., in modern COBOL: 01-05-10 and 1-2-3-... are valid).
EXAMPLE FOR RECORDS:
In case of same structure, the data of a record can be copied to another record by using the MOVE command.
After the operation, the source and the target data will be independent, if one is changed, the other will stay the same.
EXAMPLE FOR COPYING RECORD DATA:
The OCCURS creates multiple instances in memory of the defined type and renders them to the given variable name.
The size of an array is fix, the order of the elements in memory (not their values but their memory addresses) are fix as well.
The OCCURS clause can appear only on 02-49 levels, it is not allowed to define it on the 1st, therefore a record structure will include an array.
In an array the first index is 1.
Giving initial value is optional, using the VALUE clause gives the same value to all elements.
The length of the array can not be queried. If the code needs it, store the maximum element number in a variable, and use this variable in the OCCURS clause a well for better consistency.
There is no boundary check in COBOL for arrays. Accessing elements with a subscript less than 1 and more than the length will not raise error, instead, wil read memory portions based on the equation.
Indexing the array can happen by using:
subscript (classical, direct access)
index (indexed solution)
A subscript is a number which COBOL will tranform to memory address by multiplying it upon access every time which makes it a slower technique. A subscript can be a literal or a number variable. The equation is:
base_address + (subscript - 1) * element_size
An index is a special variable type to which COBOL generates an index layer over the array; this will make element accessing faster, and automated searching possible.
The optional INDEXED BY clause in the array definition specifies such index. More indexes can be rendered to the same array.
Index management:
the SET sets an element position
the UP BY and the DOWN BY clauses calculate new element position from the actual
using the MOVE raises an error
the position as number can not be processed, can not be tested in a condition, and can not be used as an iteration variable
EXAMPLE FOR EXPLICIT BOUND CHECKING FOR SUBSCRIPT:
EXAMPLE FOR INDEX MANAGEMENT:
EXAMPLE FOR DEFINITION AND ACCESS OF 1D ARRAYS 1:
EXAMPLE FOR DEFINITION AND ACCESS OF 1D ARRAYS 2:
EXAMPLE FOR DEFINITION AND ACCESS OF 2D ARRAYS (MATRIXES):
EXAMPLE FOR WRITING ARRAY TO FILE:
EXAMPLE FOR READING FILE TO ARRAY:
The SEARCH:
is linear
works with index only
does not need sorted array
in case of multiple conditions as search condition
The search method:
sets the first defined index (when the array was defined) to the first element
runs linearly through elements
when there is a match, steps into the WHEN branch
referring to an element inside the WHEN is ARRAYNAME(INDEX)
when there is no match at all, steps into the AT END branch
the AT END branch is optional
the index stays on the found or after the last element
if the array size is zero, does not run at all (defined as OCCURS 0 TIMES)
Index position, on an OCCURS 10:
upon match on the 4th element, index = 4
upon no match, index = 11
EXAMPLE FOR SEARCH, 1 INDEX:
EXAMPLE FOR SEARCH, EXAMPLE FOR SEARCH, 2 INDEXES:
The SEARCH ALL:
is binary
works with index only
needs sorted array (ascending or descending)
in case of simple condition as search condition
the array can be huge
The search method:
sets the first defined index (when the array was defined) to the first element
when there is a match, steps into the WHEN branch
referring to an element inside the WHEN is ARRAYNAME, without INDEX
the condition must be an equality (something = something)
all elements must have the same structure
the index stays on the found element or will be undefined.
For sorting an array there is no function or routine. Best practice is to write it out to a file, sort the file, then read it back to an array. See COBOL FILE OPERATIONS for information on file sorting.
EXAMPLE FOR SEARCH ALL:
The REDEFINES has two possible purposes:
to access the original memory area through an alternative variable name (and type if necessary), any simple types are valid
to parse the original memory area into more, through alternative variable names (and types if necessary), mostly A and X variables are valid
This is not copying but accessing the same memory area, therefore any changes in the value of the original or the new (parsed or not) variable(s) will have effect on the other.
For the parsing version, if an A or X variable is given (strings), with a REDEFINES structure parses it to multiple parts. The total length of the parts must cover the length of the original.
In case the length of the original value changes, the (parsed) variables will truncate or fill. For security reasons, use parsing for fixed-length variables.
EXAMPLE FOR REDEFINES:
EXAMPLE FOR REDEFINES ON TIMESTAMP:
The REDEFINES works on level-88 variables as well but because there are no fields under the REDEFINES line), this is the no-parsing version, see under other point here.
The level-88 or condition name defines a logical state which is connected to a value of a field. Literally: there is a variable in which name-value pairs are defined.
The variable can take any value from its type but is a defined key-value pair is touched, that key will get TRUE flag.
In order to set the variable on a defined value, the corresponding key must be set to true.
In case a key is set to false, COBOL will do nothing since will not know what value should be given to the level-88 variable.
It is possible to give initial value to the variable. In case this value is matching to one of the 88 lines, the corresponding 88 will be TRUE upon testing.
The level-88 is very useful for:
USING MORE READABLE IF-S
BUILDING STATUS MACHINES
DEFINING BOOLEAN VARIABLES
EXAMPLE FOR LEVEL 88:
There can be more, separated values rendered to a key, and value intervals as continuous values as well. That means for all those values the key must be true or by another words, if a value is mathcing to the value set, that key will be true.
When a key has multiple values, upon setting the key to TRUE, the variable will be set to the first value of the key.
EXAMPLE FOR MULTIPLE VALUES ON A KEY:
The level-88 variables can be used in:
IF
EVALUATE
REDEFINES
SEARCH WHEN
READ AT END
PERFORM UNTIL
etc.
The level-88 variables can be used as boolean literals as well as file handling flags (end of file), see COBOL LITERALS and COBOL VARIABLES for more on the topic.
The level-88 variables can be redefined by REDEFINES. In case the new type is given, the 88 lines will get new types, different from the corresponding original 88 lines.
EXAMPLE FOR REDEFINES ON LEVEL-88:
The level-88 works on an array, following the rules:
the level-88 is defined on the whole array upon the array definition
a key will work as an alternative array
through this alternative array the given index points to the element of the array on the same index
the element value can be set and examined by the alternative array
EXAMPLE FOR LEVEL-88 ON ARRAY:
Definition of a variable which will be a common alias for multiple fields of a record. Accessing this alias will have effect on those fields together as they were kvasi concatenated into one memory area.
The RENAMES definition follows the field definition order as they appear in the corresponding record structure definition.
In case there are the same field names in multiple record definitions, use the OF <recordname> to identify the corresponding record.
The level 66 is reserved only for RENAME definitions.
EXAMPLE FOR RENAMES:
EXAMPLE FOR RENAMES ON TIMESTAMP: