Processing multiply occurring fields and field groups: Difference between revisions
m (→Example) |
m (→Example) |
||
Line 298: | Line 298: | ||
</p> | </p> | ||
<p> | <p> | ||
generates three temporary records for the single permanent record in which the field named KEY occurs three times:</p> | generates three temporary records for the single permanent record in which the field named <code>KEY</code> occurs three times:</p> | ||
<p class="code">KEY = COMPUTER | <p class="code">KEY = COMPUTER | ||
KEY = CORPORATION | KEY = CORPORATION | ||
Line 314: | Line 314: | ||
</p> | </p> | ||
<p> | <p> | ||
These correspond to the n cyclic permutations of the set of field occurrences and, in this example, are sorted into the order 3, 1, 2 (if no other option is selected). Statements that refer to the sorted set, such as PRINT, PRINT EACH, NOTE, and PRINT ALL INFORMATION (PAI), reflects the permutation. No record is generated if n = 0.</p> | These correspond to the <var class="term">n</var> cyclic permutations of the set of field occurrences and, in this example, are sorted into the order 3, 1, 2 (if no other option is selected). Statements that refer to the sorted set, such as PRINT, PRINT EACH, NOTE, and PRINT ALL INFORMATION (PAI), reflects the permutation. No record is generated if <var class="term">n</var> = 0.</p> | ||
===EACH modifier with several key fields=== | ===EACH modifier with several key fields=== |
Revision as of 19:39, 7 April 2014
Overview
A single field name that has different values can be stored repeatedly in a record. For example, the field CHILD in the record shown below is an example of a multiply occurring field:
FATHER = JOHN DOE CHILD = ELIZABETH CHILD = ROBERT
Any field name except for the following types of fields can be present more than once in a record:
- A NUMERIC RANGE field
- A sort key
- A hash key
Special processing for multiply occurring fields
Certain User Language statements operate differently on multiply occurring fields than on singly occurring fields. User Language provides special statements and forms of statements that can be used with multiply occurring fields — specifically introducing the EACH modifier and subscripts. User Language also provides for the use of subscripted field references for accessing a particular occurrence of a multiply occurring field.
FIELDGROUP overview
FIND statement
Retrieval
Example 1
Assume this sample record:
FATHER = JOHN DOE CHILD = ELIZABETH CHILD = ROBERT
You can use either of these two statements to retrieve the record:
FIND.RECS: FIND ALL RECORDS FOR WHICH CHILD = ELIZABETH END FIND FIND.RECS: FIND ALL RECORDS FOR WHICH CHILD = ROBERT END FIND
Example 2
The record is not retrieved by either of these statements:
FIND.RECS: FIND ALL RECORDS FOR WHICH CHILD = NOT ELIZABETH END FIND FIND.RECS: FIND ALL RECORDS FOR WHICH CHILD = NOT ROBERT END FIND
Multi-condition range retrievals
You should pay special attention to the effect of multiply occurring fields on multi-condition range retrievals. Undesirable results can occur if a range search is specified for a multiply occurring field. For example:
FIND.RECS: FIND ALL RECORDS FOR WHICH CHILD IS AFTER KEN AND BEFORE PAUL END FIND
retrieves the sample record even though neither child's name in the sample is between KEN and PAUL. Model 204 evaluates each condition of the FIND separately, then combines the results of each evaluation to build the set of records satisfying all conditions. The BETWEEN operator behaves exactly like any other multi-condition range retrieval when used on a multiply occurring field.
If a range search must be performed on a multiply occurring field, use the IN RANGE clause of the FIND statement. Refer to NUMERIC RANGE and ORDERED NUMERIC attributes for more information about the IN RANGE clause. Better code for the previous example is:
FIND.RECS: FIND ALL RECORDS FOR WHICH CHILD IS ALPHA IN RANGE AFTER KEN AND BEFORE PAUL END FIND
Use of subscripts
You cannot use subscripted references with the FIND statement. Refer to Subscripts for more information about using subscripts with multiply occurring fields.
NOTE statement
Only first occurrence is noted
The NOTE statement notes only the first occurrence of a multiply occurring field. For example, only the first occurrence of the field (CHILD = ELIZABETH) are noted by:
KEEP.CHILD: NOTE CHILD
Use of subscripts
In order to note a particular occurrence of a multiply occurring field, the field name must be subscripted. Refer to the discussion of subscripts on Subscripts for detailed information on subscripted field names and usage.
PRINT and PRINT n statements
PRINT statement output format
The PRINT statement prints only the first occurrence of a field in a record.
If there is more than one value of a field in a record, the special modifier, EACH, can be used in a PRINT statement to print out all the values on a single line, with a single space between values.
Example 1
PRINT EACH INCIDENT
yields:
T1 T2 T1 T3 T2 T1
Example 2
If the field is given a column position, as in:
PRINT FULLNAME WITH EACH INCIDENT AT COLUMN 18
values are printed one to a line and positioned at the column specified.
ABBOTT, GAIL H T1 - T3 - T1 - T3 - T2 - T1
Example 3
A field cited in a PRINT statement after a multiply occurring field is printed on the same line as the last value of the multiply occurring field. If you change the PRINT statement in the sample request as here:
PRINT FULLNAME WITH EACH INCIDENT AT COLUMN 18 - WITH POLICY NO AT COLUMN 23 - WITH STATE AT COLUMN 32
This output results:
ABBOTT, GAIL H T1 - T3 - T1 - T3 - T2 - T1 100340 CALIFORNIA
Use of subscripts
See PRINT statement for a description of using PRINT with subscripts.
PRINT n statement
Long fields, such as abstracts, evaluations, or statements of purpose, can be stored as a multiply occurring field. Each occurrence can contain as many as 255 characters.
Syntax
You can use this PRINT statement to print such a field as a paragraph:
PRINT n fieldname
where n must be a number less than 32,768.
Output format
The PRINT n statement prints up to n lines of text, composed of all of the occurrences of the field concatenated in order. Nothing is inserted. At most, n lines are printed; any extra lines are ignored.
Ordinarily, a PRINT statement that produces more than one line inserts a hyphen in the continuation column of the output device. Instead of using the continuation column, this form of PRINT attempts to end each line with a complete word delimited by spaces. If there is insufficient space to fit the last word on a line, the word is hyphenated arbitrarily and continued on the next line.
The AT COLUMN and TO COLUMN clauses can be used to adjust the output to a narrower column. When used together with PRINT n, text is broken at word boundaries to fit within the column. The AT clause does not affect the first line, and the TO clause does not affect the last line (unless the line limit n is exceeded). This allows indenting.
The AT or TO column options cannot accept negative numbers or numbers greater than 32767 for the column.
Procedures containing PRINT statements with negative numbers or numbers greater than 32767 fail at compile time with the following counting error message:
M204.0263: AT/TO MUST BE BETWEEN 1 AND 32767
Example
This example illustrates the use of the PRINT n statement.
BEGIN PRINT '1234567890123456' STORE RECORD TEXT = 'NOW IS THE T' TEXT = 'IME FOR' TEXT = ' ALL GOOD MEN TO' ITEM = 1 END STORE FD.REC: FIND ALL RECORDS FOR WHICH ITEM = 1 END FIND FOR 1 RECORD IN FD.REC PRINT.TEXT: PRINT '' AT COLUMN 5 WITH 3 TEXT - AT COLUMN 3 TO 16 END FOR END
The output produced is:
1234567890123456 NOW IS THE TIME FOR ALL GOOD MEN TO
The PRINT.TEXT
statement concatenates a null value (two single quotes with no space between them) with the PRINT n form to indent the first line of text by using the location of the null value (column 5).
Note that the printing stops short of column 16 to avoid truncating TIME and GOOD.
Use with subscripts
You cannot use subscripted references with the PRINT n statement. Refer to Subscripts for more information about using subscripts with multiply occurring fields.
ADD, CHANGE, and DELETE statements
The ADD and CHANGE statements, and both forms of the DELETE statement, are supported in remote file and scattered group contexts.
ADD statement
The ADD statement places new occurrences of a field after existing occurrences. For example, if new children are added to the sample record, the additions are placed last. Thus:
ADD CHILD = SARAH ADD CHILD = PATRICK
results in the record:
FATHER = JOHN DOE CHILD = ELIZABETH CHILD = ROBERT CHILD = SARAH CHILD = PATRICK
The INSERT statement
If the order of occurrence is important, the INSERT statement can be used to add new occurrences. See the discussion on INSERT statement.
CHANGE statement
The CHANGE statement alters only the first occurrence of a field in a record.
Syntax
You can use this form of the CHANGE statement if there is more than one occurrence:
CHANGE fieldname = value1 TO value2
This form, like the CHANGE fieldname statement, can be used only inside a FOR EACH RECORD loop. It deletes the first occurrence of the pair fieldname = value1 from a record, and adds the pair fieldname = value2 to the record. The new value is added either in the position occupied by the original value or at the end of the record, depending upon the update attribute specified for the field by the file manager. See the discussion in UPDATE field attribute.
If the specified field, fieldname = value1, does not appear in the record, fieldname = value2 is simply added to the record. If the specified fieldname = value1 pair appears more than once in the record, only the first occurrence of it is deleted. The pair, fieldname = value2, is added just once. Occurrences of the field name that have other values are not altered by the statement.
DELETE statement
The DELETE statement deletes only the first occurrence of the field in the record by default.
Two forms of DELETE statement
For multiply occurring fields, two forms of the DELETE statement are provided; however only for use inside a FOR EACH RECORD loop:
To delete... | Syntax | Usage |
---|---|---|
A particular occurrence | DELETE fieldname = value |
To delete the first occurrence of the pair, fieldname = value, from a record. Occurrences of the field that have other values are not removed. If the field with the specified value: Occurs more than once in the record, only the first occurrence is deleted. Cannot be found, no deletion occurs. |
Every occurrence of the field in the record | DELETE EACH fieldname | To delete all occurrences of the specified field name. The field to be deleted cannot have the INVISIBLE attribute. |
Use with the FOR EACH OCCURRENCE statement
See Deleting occurrences for a discussion of deleting occurrences using the FOR EACH OCCURRENCE OF (FEO) statement.
Use with subscripts
See DELETE statement for a discussion of using the DELETE statement with subscripts.
SORT RECORDS statement
When a multiply occurring field is chosen as a sort key, each record in the set being sorted is processed once using the first occurrence of the field as the key.
EACH modifier with one sort field
If the EACH modifier is present in the SORT statement and if there are n occurrences of the field in the record, similar records are created in the sorted copy of the original set.
Example
The following request:
SORT RECORDS IN FIND.RECS BY EACH KEY
generates three temporary records for the single permanent record in which the field named KEY
occurs three times:
KEY = COMPUTER KEY = CORPORATION KEY = AMERICA
The records generated are:
1 KEY = COMPUTER 2 KEY = CORPORATION KEY = CORPORATION KEY = AMERICA KEY = AMERICA KEY = COMPUTER 3 KEY = AMERICA KEY = COMPUTER KEY = CORPORATION
These correspond to the n cyclic permutations of the set of field occurrences and, in this example, are sorted into the order 3, 1, 2 (if no other option is selected). Statements that refer to the sorted set, such as PRINT, PRINT EACH, NOTE, and PRINT ALL INFORMATION (PAI), reflects the permutation. No record is generated if n = 0.
EACH modifier with several key fields
When the EACH option is selected for several keys, n occurrences of one key and m of another produce differently permuted records. If either n or m equal 0, no records are generated.
Example
This request:
BEGIN FIND.RECS: IN CLIENTS FIND ALL RECORDS FOR WHICH INCIDENT = T1 END FIND SORT.RECS: SORT RECORDS IN FIND.RECS BY FULLNAME - AND EACH INCIDENT DATE FOR EACH RECORD IN SORT.RECS PRINT FULLNAME WITH INCIDENT DATE - AT COLUMN 25 END FOR END
produces printed output of:
ABBOTT, FRANKLIN G 860323 ABBOTT, GAIL H 861022 ABRAMS, RUTH Z 861115 ABRAMS, RUSSELL Y 870218 ABBOTT, FRANKLIN G 870424 ABBOTT, GAIL H 871123 . . . . . .
If no occurrences are present
A record that does not contain at least one occurrence of the INCIDENT DATE field produces no printed output. Similarly:
SORT RECORDS IN FIND.RECS BY EACH A AND EACH B
would produce nothing for those records that do not have at least one occurrence of both A and B.
FOR EACH RECORD statement
The FOR EACH RECORD IN ORDER BY statement retrieves and loops on only the first occurrence of a field in a record.
EACH modifier
If there is more than one value of a field in a record, the special modifier, EACH, can be used to retrieve and loop on all values of the field.
The EACH modifier only can be used on a FOR EACH RECORD statement that specifies index order processing (the IN ORDER BY fieldname clause must be used and the field must have the ORDERED attribute). The VALUE IN phrase must be used to retrieve the current value of the ORDERED field driving the loop.
Example
This example of the FOR EACH RECORD IN ORDER BY statement:
BEGIN FR1: FOR EACH RECORD IN ORDER BY EACH INCIDENT DATE PRINT VALUE IN FR1 WITH FULLNAME AT COLUMN 25 END FOR END
returns each record in order by each value of the ORDERED field, as follows:
760323 ABBOTT, FRANKLIN G 761022 ABBOTT, GAIL H 761115 ABRAMS, RUTH Z 770218 ABRAMS, RUSSELL Y . . .
Special statements for multiply occurring fields and field groups
User Language provides several statements for handling multiply occurring fields and field groups. Field groups are supported as of Model 204 version 7.5.
- COUNT OCCURRENCES (used with singly occurring fields, too)
- FOR ALL OCCURRENCES (FAO) statement
- FOR EACH OCCURRENCE (used with singly occurring fields, too)
- FOR FIELDGROUP
- DELETE EACH OCCURRENCE
Note: See INSERT statement, which you can use to add new occurrences of a field.
COUNT OCCURRENCES OF statement
The COUNT OCCURRENCES OF (CTO) statement counts the number of occurrences of the named field in the current record.
Syntax
The format of the COUNT OCCURRENCES statement is:
label: COUNT OCCURRENCES OF fieldname
or
label: CTO fieldname
The field name cannot be subscripted. The COUNT OCCURRENCES OF statement can appear only within a FOR EACH RECORD loop.
The COUNT OCCURRENCES statement is supported in remote file and scattered group contexts.
COUNT IN clause
The COUNT IN clause refers to the count obtained by the COUNT OCCURRENCES OF statement. This request:
BEGIN FIND.RECS: IN VEHICLES FIND ALL RECORDS END FIND FOR 5 RECORDS IN FIND.RECS NO.OF.VINS: COUNT OCCURRENCES OF VIN PRINT OWNER POLICY WITH ' INSURES ' WITH - COUNT IN NO.OF.VINS WITH ' VEHICLE(S)' END FOR END
generates the following output:
100025 INSURES 1 VEHICLE(S) 100030 INSURES 1 VEHICLE(S) 100032 INSURES 1 VEHICLE(S) 100051 INSURES 1 VEHICLE(S) 100058 INSURES 1 VEHICLE(S)
FOR ALL OCCURRENCES OF FIELDGROUP (FAO FIELDGROUP) statement
The FAO statement collects the field group IDs for all occurrences, then processes against that list.
Syntax
The format of the statement is:
FOR ALL OCCURRENCES OF FIELDGROUP (fgname | %%varGrp)
Where:
- fgname specifies a field group name.
- %%varGrp specifies a field group name variable.
The difference between an FAO statement and an FEO statement is that the FAO statement is less affected by insertions or deletions of FIELDGROUP fgname within the loop, due to the semantics of field group IDs. This means that deleting the current occurrence within the loop will not affect occurrences referenced in subsequent iterations.
FOR EACH OCCURRENCE OF loops
On each loop of FOR EACH OCCURRENCE OF (FEO), the VALUE IN and OCCURRENCE IN labels refer to value and position, respectively, of the next field occurrence, starting with occurrence 1 and increasing by 1 for each pass through the loop. When the next field occurrence number is greater than the number of field occurrences in the record, the loop terminates.
Syntax
The format of the FOR EACH OCCURRENCE loop is:
label: FOR EACH OCCURRENCE OF fieldname
Alternatively,
label: FEO fieldname
The FOR EACH OCCURRENCE OF statement is supported in remote file and scattered group contexts.
Example
Consider this record:
FIRST NAME = RICHARD LAST NAME = SMITH CHILD = HENRY CHILD = SALLY CHILD = JANE ADDRESS = AVON DRIVE
This request creates a separate record for each child.
BEGIN FD.REC: FIND ALL RECORDS FOR WHICH FIRST NAME = RICHARD LAST NAME = SMITH END FIND FOR EACH RECORD IN FD.REC NOTE.ADD: NOTE ADDRESS CHILD.LOOP: FOR EACH OCCURRENCE OF CHILD PRINT VALUE IN CHILD.LOOP STORE RECORD FIRST NAME = VALUE IN CHILD.LOOP LAST NAME = SMITH ADDRESS = VALUE IN NOTE.ADD END STORE END FOR END FOR END
Simulating a FOR EACH VALUE loop
The FOR EACH OCCURRENCE OF statement can be used to simulate a FOR EACH VALUE loop if the field contains a static collection of known values. Consider the values of states. The user sets up a single record that has a multiply occurring field in sorted sequence as follows:
TYPE = STATE CODE = ALABAMA CODE = ARKANSAS . . CODE = WYOMING
These statements begin a request to generate a report in order by state:
BEGIN FIND.TYPE: FIND ALL RECORDS FOR WHICH TYPE = STATE END FIND FOR EACH RECORD IN FIND.TYPE EACH.CODE: FOR EACH OCCURRENCE OF CODE FIND.STATE: FIND ALL RECORDS FOR WHICH STATE = VALUE IN EACH.CODE END FIND FOR EACH RECORD IN FIND.STATE . .
This method has the advantage of eliminating the internal sort required by FOR EACH VALUE IN ORDER and provides an easy way to simulate a FOR EACH VALUE loop for a field that does not have the FRV or ORDERED attribute. However, a change in the list of values can require a recreation of the TYPE record to keep the values in order. Recreation is not required if the UPDATE IN PLACE field attribute has been specified for the CODE field and if the new and old values occupy the same place in order. Recreation also is not required if the INSERT statement is used correctly.
Deleting occurrences
The DELETE EACH statement can be used to delete all occurrences.
The FOR EACH OCCURRENCE OF loop, when used to delete occurrences, deletes the first 50% of occurrences of a multiply occurring field on the current record, in the order that they were stored on that record. If there are an odd number of occurrences of the field on the record, then the first up to and including the middle occurrence will be deleted. With each pass through the loop, the fields and records in Table B are shifted to eliminate the space the deleted occurrence took, condensing Table B storage. The pointer is also shifted to point to the first occurrence in the remaining items.
Example
Suppose the following statement is specified for the RICHARD SMITH record described previously:
FOR EACH RECORD IN FD.REC DEL.CHILD: FOR EACH OCCURRENCE OF CHILD DELETE CHILD END FOR END FOR
The record originally contained these CHILD entries:
CHILD=HENRY CHILD=SALLY CHILD=JANE
On the first pass through the loop, the first value, HENRY, is selected and deleted. At the end of the first pass, the record contains these CHILD entries:
CHILD=SALLY CHILD=JANE
On the second pass through the loop, the DELETE CHILD statement again deletes the first occurrence of the field as described in DELETE statement. Since HENRY has already been deleted, Model 204 deletes the first entry, SALLY.
After the second pass, the FOR EACH OCCURRENCE loop terminates. This is because the value in DEL.CHILD should be the next (third) occurrence, but after two passes, only one occurrence remains on the record. Therefore, at the end of the FOR EACH OCCURRENCE loop, the remaining value in the CHILD field is:
CHILD=JANE
Deleting with an FEO FIELDGROUP versus FAO FIELDGROUP loop
An FEO FIELDGROUP statement on field groups has the same semantics as an FEO statement on a single field. When you iterate in an FEO FIELDGROUP or FEO loop, the next occurrence processed is the occurrence number one greater than the one processed before, whether or not it is one you have already processed (because of insertions) or whether or not you skip one or more (because of deletions). For example:
- To delete every other occurrence of the field group, use an FEO statement:
FEO FIELDGROUP VEHICLE DELETE FIELDGROUP END FOR
- To delete all occurrences of the field group, use an FAO FIELDGROUP statement:
FAO FIELDGROUP VEHICLE DELETE FIELDGROUP END FOR
FOR FIELDGROUP statement
The FOR FIELDGROUP statement operates on a specific occurrence of a field group. The occurrence is indicated by either an occurrence number or a field group ID number.
Syntax
The format of the statement is:
FOR FIELDGROUP (fgname | %%varFgname) { (occurrence number) | = fieldgroupID }
Where:
- fgname specifies a field group name
- %%varGrp specifies a field group name variable
- (occurrence number) specifies an occurrence within that field group. The occurrence number must be enclosed in parentheses.
- =fieldgroupID specifies a field group ID. fieldgroupID must be preceded by an equal sign.
Example
FR WHERE ... FOR FIELDGROUP DRIVER (3) PRINT DRIVER_ID END FOR END FOR FR WHERE ... %DRIVER = 'DRIVER' FOR FIELDGROUP %%DRIVER = %FGID PRINT DRIVER_ID AND DRIVER_NAME END FOR END FOR
VALUE IN with FOR EACH OCCURRENCE loops
VALUE IN references to FOR EACH OCCURRENCE (FEO) statements from outside the FEO loop does not compile. Such references receive the following error message:
M204.0311 UNACCEPTABLE STATEMENT REFERENCE
At runtime, the space occupied in STBL by the FEO value is reclaimed after each iteration of the FEO loop (including the last). This results in a reduction in the runtime STBL space requirements of some programs that use FEO.
To avoid getting that error, move the value into a %variable inside the FEO loop, and then reference the %variable outside the FEO loop.
FOR EACH OCCURRENCE OF against INVISIBLE fields
Using FOR EACH OCCURRENCE (FEO) syntax against INVISIBLE fields is a waste of processing time. An FEO causes a scan of the current Table B record, but INVISIBLE fields are not stored in Table B, so an FEO against an INVISIBLE field can never find any occurrences. Prior to V4R2.0 this programming flaw was masked because using FEO syntax against an INVISIBLE field compiled and evaluated successfully, although it never found occurrences.
Beginning in V4R2.0, FEO syntax against an INVISIBLE field results in compilation error:
M204.0320 FIELD IS INVISIBLE. FIELD =
Setting field group context
Using a FOR ALL OCCURRENCES OF loop
You can set field group context with a FOR ALL OCCURRENCES OF (FAO) loop. For example:
BEGIN IN POLICIES FOR EACH RECORD FAO: FAO FIELDGROUP VEHICLE IF COLOR IS LIKE '*RED*' THEN PRINT 'POLICY:' AT 5 AND POLICY_NUMBER AND - 'COLOR=' WITH COLOR %RED = %RED + 1 END IF END FOR END FOR PRINT 'NUMBER OF RED CARS:' AND %RED END POLICY: 100001 COLOR=VICTORY RED POLICY: 100007 COLOR=RED POLICY: 100011 COLOR=VICTORY RED ... NUMBER OF RED CARS: 214 ...and so on...
Using a FOR EACH OCCURRENCE loop
You can also set field group context with a FOR EACH OCCURRENCE (FEO) loop on the field group. The FEO loop establishes a field group context. References to fields in the field group inside the loop refer to occurrences in that field group. For example:
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 FEO: FEO FIELDGROUP VEHICLE %DRIVER_ID = OTHER_DRIVER PRINT OCCURRENCE IN FEO AND MAKE AND MODEL AND - 'OTHER DRIVER(S):' AT 30 AND - EACH OTHER_DRIVER PRINT 'FIRST:' AT 30 AND %DRIVER_ID AND - 'THIRD:' AND OTHER_DRIVER(3) END FOR END FOR END 1 AUDI A4 QUATTRO OTHER DRIVER(S): 100035 100037 100036 FIRST: 100035 THIRD: 100036 2 CADILLAC SEVILLE OTHER DRIVER(S): FIRST: THIRD:
Missing data in field groups
As with non-field group field references for missing occurrences, the field values are returned as null. The exception is EXACTLY-ONE fields in field groups, where the default value is returned for missing occurrences.
Handling nested field group context
If field groups are nested, one can always retrieve an EXACTLY-ONE field in a field group inside the current context without establishing that nested field groups context.
In addition to retrieving field values inside an FEO FIELDGROUP loop, fields in that field group can be updated, that is added, inserted, changed and deleted:
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100095 PRINT POLICY_NUMBER FEO FIELDGROUP VEHICLE PRINT MAKE AT 5 AND MODEL PRINT '1)' AND CLAIM_NUMBER PRINT '2)' AND CLAIM_NUMBER(2) PRINT '3)' AND CLAIM_NUMBER(3) END FOR END FOR END 100095 VOLKSWAGEN NEW BEETLE 1) 2) 3) MITSUBISHI ECLIPSE 1) 100059 2) 100064 3) CHEVROLET SUBURBAN 1) 2) 3) BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 FEO FIELDGROUP VEHICLE ADD MAKE = 'XXX' INSERT MAKE(1) = %NOTHING DELETE MAKE CHANGE COLOR(4) TO 'YYYY' END FOR BACKOUT END *** 1 M204.2853: ADD NOT ALLOWED FOR EXACTLY-ONE FIELD == 'XXX' *** 2 M204.0228: PART OF STATEMENT IGNORED *** 3 M204.2853: INSERT NOT ALLOWED FOR EXACTLY-ONE FIELD == %NOTHING *** 4 M204.0228: PART OF STATEMENT IGNORED *** 5 M204.2853: DELETE NOT ALLOWED FOR EXACTLY-ONE FIELD *** M204.1042: COMPILATION ERRORS
Handling invalid occurrence numbers
The following PRINT MAKE(3) statement prints a null and without error, even if MAKE was defined as EXACTLY-ONE. This is consistent with Model 204 behavior for non-field group EXACTLY-ONE and OCCURS fields. That is, you can request occurrence 4 of an EXACTLY-ONE or OCCURS 1 field in the record and Model 204 returns a null.
Model 204 ignores invalid occurrence numbers such as negative numbers or 0. 0 is treated the same as 1.
EXACTLY-ONE fields in specific field groups (as opposed to FIELDGROUP *) can be retrieved outside the field group context. In the following example, MAKE and MODEL are EXACTLY-ONE fields in field group DRIVER in field POLICIES.
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 PRINT '1' AND MAKE AND MODEL PRINT '2' AND MAKE(2) AND MODEL(2) PRINT '3' AND MAKE(3) AND MODEL(3) END FOR END 1 AUDI A4 QUATTRO 2 CADILLAC SEVILLE 3 BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100095 PRINT POLICY_NUMBER FEO FIELDGROUP VEHICLE PRINT MAKE AT 5 AND MODEL PRINT '1)' AND CLAIM_NUMBER PRINT '2)' AND CLAIM_NUMBER(2) PRINT '3)' AND CLAIM_NUMBER(3) END FOR END FOR END 100095 VOLKSWAGEN NEW BEETLE 1) 2) 3) MITSUBISHI ECLIPSE 1) 100059 2) 100064 3) CHEVROLET SUBURBAN 1) 2) 3) BEGIN %GLASSES IS STRING LEN 50 %GLASSES = 'CORRECTIVE LENSES REQUIRED' IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 FEO FIELDGROUP DRIVER PRINT 'BEFORE ..' PAFGI ADD DRIVER_RESTRICTIONS = 'MEDICAL RESTRICTIONS PRESENT' INSERT DRIVER_RESTRICTIONS(3) = %GLASSES DELETE DRIVER_RESTRICTIONS(4) CHANGE DRIVER_MARITAL_STATUS TO 'WIDOWED' PRINT 'AFTER ...' PAFGI END FOR BACKOUT END BEFORE .. \DRIVER = 1 DRIVER_ID = 100034 DRIVER_NAME = CUMMINGS, BETTY S DRIVER_MARITAL_STATUS = SINGLE DRIVER_GENDER = F DRIVER_DATE_OF_BIRTH = 19791225 /DRIVER = 1 AFTER ... \DRIVER = 1 DRIVER_ID = 100034 DRIVER_NAME = CUMMINGS, BETTY S DRIVER_MARITAL_STATUS = WIDOWED DRIVER_GENDER = F DRIVER_DATE_OF_BIRTH = 19791225 DRIVER_RESTRICTIONS = MEDICAL RESTRICTIONS PRESENT DRIVER_RESTRICTIONS = CORRECTIVE LENSES REQUIRED /DRIVER = 1 BEFORE .. \DRIVER = 2
Constraint violations for field groups
For ADD, INSERT, and DELETE statements, you can get an EXACTLY-ONE constraint violation, as well as other field constraint violations. In the previous example, had you attempted to add, insert, or delete the field DRIVER_ID, you would have received the following error message:
M204.2853: (ADD/DELETE/INSERT) NOT ALLOWED FOR EXACTLYONE FIELD
In general, the behavior of updating statements mimics the Model 204 handling of exception cases. For example, a DELETE of an occurrence that is not there is ignored. A CHANGE statement for an absent occurrence becomes an ADD statement.
Referencing fields in nested field groups
You can reference a field from the containing context even inside a field group context. For example:
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 FEO FIELDGROUP DRIVER PRINT POLICY_NUMBER AND DRIVER_NAME END FOR END 100013 CUMMINGS, BETTY S 100013 CUMMINGS, EDDIE R 100013 CUMMINGS, LEE V 100013 CUMMINGS, MARY U 100013 CUMMINGS, ROBERT T
In other words, you can delete every other occurrence of a field groups:
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 PRINT POLICY_NUMBER PRINT 'BEFORE...' FEO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR FEO FIELDGROUP DRIVER DELETE FIELDGROUP END FOR PRINT 'AFTER....' FEO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR BACKOUT END 100013 BEFORE... CUMMINGS, BETTY S CUMMINGS, EDDIE R CUMMINGS, LEE V CUMMINGS, MARY U CUMMINGS, ROBERT T AFTER.... CUMMINGS, EDDIE R CUMMINGS, MARY U *** M204.1099: TRANSACTION 1 HAS BEEN BACKED OUT
The following code example shows the difference when you use an FAO statement instead of an FEO statement.
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 PRINT POLICY_NUMBER PRINT 'BEFORE...' FAO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR FAO FIELDGROUP DRIVER DELETE FIELDGROUP END FOR PRINT 'AFTER....' FAO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR BACKOUT END 100013 BEFORE... CUMMINGS, BETTY S CUMMINGS, EDDIE R CUMMINGS, LEE V CUMMINGS, MARY U CUMMINGS, ROBERT T AFTER.... *** M204.1099: TRANSACTION 2 HAS BEEN BACKED OUT BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100095 PRINT '...BEFORE...' FAO FIELDGROUP VEHICLE PRINT MAKE AND MODEL END FOR PRINT 'ADD A HONDA PILOT ...' ADD FIELDGROUP VEHICLE MAKE = HONDA MODEL = PILOT * ... OTHER VEHICLE FIELDS END ADD PRINT '...AFTER....' FAO FIELDGROUP VEHICLE PRINT MAKE AND MODEL END FOR BACKOUT END ...BEFORE... VOLKSWAGEN NEW BEETLE MITSUBISHI ECLIPSE CHEVROLET SUBURBAN ADD A HONDA PILOT ... ...AFTER.... VOLKSWAGEN NEW BEETLE MITSUBISHI ECLIPSE CHEVROLET SUBURBAN HONDA PILOT *** M204.1099: TRANSACTION 6 HAS BEEN BACKED OUT
FEO loops and field group context
On each iteration of a field group FEO loop, the current field group is set as the field group context. If that field group is deleted and a field in that field group is referenced you get a request cancelling error.
FEO on field groups has the same semantics as those on a single field. When you iterate in an FEO FIELDGROUP, the next occurrence processed is the occurrence number one greater than the one processed before, whether or not it is one you have already processed (because of insertions) or whether or not you skip one or more (because of deletions).
In other words, you can delete every other occurrence of a field group:
BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 FEO FIELDGROUP DRIVER PRINT POLICY_NUMBER AND DRIVER_NAME END FOR END 100013 CUMMINGS, BETTY S 100013 CUMMINGS, EDDIE R 100013 CUMMINGS, LEE V 100013 CUMMINGS, MARY U 100013 CUMMINGS, ROBERT T BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 PRINT POLICY_NUMBER PRINT 'BEFORE...' FEO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR FEO FIELDGROUP DRIVER DELETE FIELDGROUP END FOR PRINT 'AFTER....' FEO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR BACKOUT END 100013 BEFORE... CUMMINGS, BETTY S CUMMINGS, EDDIE R CUMMINGS, LEE V CUMMINGS, MARY U CUMMINGS, ROBERT T AFTER.... CUMMINGS, EDDIE R CUMMINGS, MARY U *** M204.1099: TRANSACTION 1 HAS BEEN BACKED OUT SHOW DIFFERENCE WHEN YOU USE FAO INSTEAD OF FEO: BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100013 PRINT POLICY_NUMBER PRINT 'BEFORE...' FAO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR FAO FIELDGROUP DRIVER DELETE FIELDGROUP END FOR PRINT 'AFTER....' FAO FIELDGROUP DRIVER PRINT DRIVER_NAME AT 5 END FOR BACKOUT END 100013 BEFORE... CUMMINGS, BETTY S CUMMINGS, EDDIE R CUMMINGS, LEE V CUMMINGS, MARY U CUMMINGS, ROBERT T AFTER.... *** M204.1099: TRANSACTION 2 HAS BEEN BACKED OUT BEGIN IN POLICIES FOR EACH RECORD WHERE POLICY_NUMBER = 100095 PRINT '...BEFORE...' FAO FIELDGROUP VEHICLE PRINT MAKE AND MODEL END FOR PRINT 'ADD A HONDA PILOT ...' ADD FIELDGROUP VEHICLE MAKE = HONDA MODEL = PILOT * ... OTHER VEHICLE FIELDS END ADD PRINT '...AFTER....' FAO FIELDGROUP VEHICLE PRINT MAKE AND MODEL END FOR BACKOUT END ...BEFORE... VOLKSWAGEN NEW BEETLE MITSUBISHI ECLIPSE CHEVROLET SUBURBAN ADD A HONDA PILOT ... ...AFTER.... VOLKSWAGEN NEW BEETLE MITSUBISHI ECLIPSE CHEVROLET SUBURBAN HONDA PILOT *** M204.1099: TRANSACTION 6 HAS BEEN BACKED OUT
UPDATE field attribute
Impact of changing a value
When the user changes the value of a field, how Model 204 changes the occurrence depends upon whether the field was defined with the UPDATE IN PLACE or UPDATE AT END attribute, as follows:
Attribute | How the update works... |
---|---|
UPDATE IN PLACE (the default) | The value of the field occurrence is changed but its position in the record is preserved. To change the order of values, the user must delete the old value and add the new one in separate statements. |
UPDATE AT END | The existing occurrence is deleted and the new one is automatically added at the end. UPDATE AT END is normally specified for applications that depend on value rotation to accomplish aging. |
Example
This example includes both approaches to updating. Suppose a record has the following fields:
NAME = RICHARD SMITH CHILD = HENRY CHILD = SALLY CHILD = JANE
You could use the technique illustrated below to add a last name to each child. (This technique involves the use of %variables, which are discussed in Using variables and values in computation.)
BEGIN FIND.RECS: FIND ALL RECORDS FOR WHICH NAME = RICHARD SMITH END FIND FOR EACH RECORD IN FIND.RECS EACH.CHILD FOR EACH OCCURRENCE OF CHILD %A = VALUE IN EACH.CHILD WITH ' SMITH' CHANGE CHILD = VALUE IN EACH.CHILD TO %A END FOR END FOR END
If the UPDATE IN PLACE option is specified
If the UPDATE IN PLACE option has been specified for the CHILD field, then the FOR EACH OCCURRENCE loop change each occurrence of CHILD in turn.
On the first pass through the loop, the first value, HENRY, is selected and changed to HENRY SMITH. After the first pass, the record looks like this:
NAME = RICHARD SMITH CHILD = HENRY SMITH CHILD = SALLY CHILD = JANE
At the end of the second pass, SALLY is changed:
NAME = RICHARD SMITH CHILD = HENRY SMITH CHILD = SALLY SMITH CHILD = JANE
At the end of the third pass, JANE is changed:
NAME = RICHARD SMITH CHILD = HENRY SMITH CHILD = SALLY SMITH CHILD = JANE SMITH
If the UPDATE AT END option is specified
If the UPDATE AT END option has been specified for the CHILD field, the FOR EACH OCCURRENCE loop proceeds as described here.
On the first pass through the FOR EACH OCCURRENCE loop the first value, HENRY, is selected and changed to HENRY SMITH. The act of changing, however, causes the value HENRY to be deleted and the value HENRY SMITH to be added as the last child. Thus after the first pass, the record looks like this:
NAME = RICHARD SMITH CHILD = SALLY CHILD = JANE CHILD = HENRY SMITH
On the second pass through the loop, JANE, which is now the second occurrence of CHILD, is deleted and JANE SMITH added to the end of the record:
NAME = RICHARD SMITH CHILD = SALLY CHILD = HENRY SMITH CHILD = JANE SMITH
On the third pass, the third occurrence is JANE SMITH, and the record ends with:
NAME = RICHARD SMITH CHILD = SALLY CHILD = HENRY SMITH CHILD = JANE SMITH
Subscripts
Subscripts can be included in Model 204 field references to facilitate the selection of particular occurrences of multiply occurring fields. Any field name can be followed by a parenthesized expression. The value of this expression is used as an ordinal number which specifies the desired occurrence of the named field. For example:
INCIDENT(3) COURSE.NUMBER(2) TRANSACTION(A+B)
Example
This request illustrates a class schedule where subscripts are used to change the room number for a course:
BEGIN COURSE: FIND ALL RECORDS FOR WHICH REC = ROOM ASSIGNMENT END FIND CHANGE: FOR EACH RECORD IN COURSE IF ROOM(1) EQ '214A' THEN CHANGE ROOM(1) TO '566A' END IF IF ROOM(2) EQ '214A' THEN CHANGE ROOM(2) TO '566A' END IF END FOR END
Subscripted field extraction
Subscripted field references attempt to maintain their position inside a record much as an FEO loop attempts to maintain its position inside a record. This means that subscripted field references tend to be as efficient as FEO loops.
The following is an example of using subscripted field extraction for a repeating field group:
%INC IS STRING ARRAY (12) NO FS %IDATE IS STRING ARRAY (12) NO FS FEOIDATA: FEO INCIDENT %INC(OCCURRENCE IN FEOIDATA) = VALUE IN FEOIDATA %IDATE(OCCURRENCE IN FEOIDATA) = INCIDENT DATE(OCCURRENCE IN FEOIDATA) END FOR
If you use multiple FEO loops for field group processing, it is possible that using this technique will require additional VTBL resources for procedures with an extremely large number of sorts or subscripted field references.
Evaluation of subscript expressions
The evaluation of subscript expressions is subject to the rules for determining an integer result for an arithmetic expression as described in Arithmetic operations.
Statements and phrases with which you cannot use subscripts
Subscripted field references can appear anywhere that unsubscripted references can, except in the following statements and phrases:
Statements you cannot use with subscripts | Because... |
---|---|
ADD | Adds a new occurrence of a field. |
BY EACH phrase in the SORT RECORDS | Loops through all occurrences of a field. |
DELETE EACH statement | Loops through all occurrences of a field. |
EACH phrase in a PRINT specification | Loops through all occurrences of a field. |
FILE | Deals with fields having the INVISIBLE attribute, which cannot be the object of subscripted references. |
FIND | Locates records without regard to which occurrence of a field contains the desired value. |
FOR EACH OCCURRENCE | Loops through all occurrences of a field. Field references within FOR EACH OCCURRENCE loops can be subscripted, depending upon the individual statements in which the references appear. |
FOR EACH VALUE | Loops through all values of all occurrences of the specified field. Field references within FOR EACH VALUE loops are not allowed at all, unless the field reference is embedded in a nested FOR EACH RECORD loop. See Setting up a value loop on one field and printing a value of another for more information. |
IN ORDER BY EACH phrase in the FOR EACH RECORD | Loops through all occurrences of a field. |
PRINT n | Loops through all occurrences of a field. |
STORE RECORD | Adds fields in the order of appearance in the statement. |
Unsubscripted field references
An unsubscripted field reference in a context in which subscripted references are allowed is always equivalent to a subscripted reference with a value of one.
Do not use subscripts with INVISIBLE fields
You cannot make subscripted references to fields that have the INVISIBLE attribute (see the discussion on field attributes in Field attributes). These fields are not truly multiply occurring, although they can have several different values in a single record. A subscript specified for a field with the INVISIBLE attribute is ignored.
Subscript validity rules
The rules presented below indicate whether or not a subscript value is valid and what action to take if the value is not valid. These rules take into account:
- The value of the subscript
- The context in which the subscript appears
- The description of the subscripted field
Explanation of the rules
In these rules, two quantities are used:
- N is the maximum number of occurrences that can be stored in a record for a given field. For a preallocated field, N equals the value of n in the OCCURS clause of the field's description. For other fields, N has no limit.
- P is the number of nonempty occurrences of the referenced field found in the specified record when the reference is evaluated.
For a summary of rules for preallocated fields, refer to the discussion on preallocated fields in Storing values in preallocated fields.
INSERT statement
The INSERT statement, like the ADD statement, is used for adding new occurrences of a field. INSERT is used to add occurrences when the order of the values is important and the values are added out of order.
The INSERT statement is supported in remote file and scattered group contexts. The INSERT statement is not supported for Large Object fields.
Syntax
The format of the INSERT statement is:
INSERT fieldname [(subscript)] = value
Example
Assume a record with these fields:
DEPT = PERSONNEL DEPT = FINANCE DEPT = MARKETING
The following statement:
INSERT DEPT(3) = ACCOUNTING
results in this record:
DEPT = PERSONNEL DEPT = FINANCE DEPT = ACCOUNTING DEPT = MARKETING
Subscript validity rules
INSERT statement lists validity rules for subscripts. In this table:
- P is the number of occurrences of the field in the record when the INSERT statement is issued.
- S is the subscript specified in the INSERT statement.
Subscript validation rules If P and S values are: Then Model 204 takes this action: P > S Inserts the new occurrence in front of the former Sth occurrence; the new occurrence becomes the current Sth occurrence of the field. P < S Inserts the new occurrence of the field after the Pth occurrence; the new occurrence becomes the (P+1) occurrence. P = 0 Adds the new value at the end of the record, as in the ADD statement. S = 0, or no subscript Treats the new value as if S = 1 and inserts the new value as the first occurrence, in front of any former occurrence of the field. S < 0 Does not add a new occurrence.
For fields with the INVISIBLE attribute, only the index is affected.
PRINT statement
In retrieval statements such as PRINT, subscript values less than zero or greater than P are invalid. Invalid references of this kind cause the null value to be returned. A subscript of zero returns the value of the first occurrence.
DELETE statement
In the DELETE statement, subscript values less than one or greater than P are invalid. If an invalid reference of this kind is made, no action is taken.
If several occurrences of a field are being deleted, you should be careful not to use DELETE in the following way:
FOR EACH RECORD IN FIND.RECS DELETE CLIENT(1) DELETE CLIENT(2) DELETE CLIENT(3) END FOR
As the statements are executed, Model 204 deletes the first occurrence, CLIENT(1), then locates the current second occurrence, which is the original CLIENT(3), and deletes it. Then, because a third occurrence cannot be found, the operation stops, and the original CLIENT(2) is never deleted.
In such a situation, deleting the occurrences in the reverse order achieves the desired result:
FOR EACH RECORD IN FIND.RECS DELETE CLIENT(3) DELETE CLIENT(2) DELETE CLIENT(1) END FOR
The desired result also can be achieved by completely omitting the subscripts, as follows:
FOR EACH RECORD IN FIND.RECS DELETE CLIENT DELETE CLIENT DELETE CLIENT END FOR
CHANGE statement
In the CHANGE statement, subscript values less than one or greater than P are treated as attempts to add a new occurrence (P+1). If (P+1) does not exceed N (the maximum number of occurrences that can be stored), the new occurrence is added to the record. If (P+1) does exceed N, the request is cancelled.
SORT RECORDS statement
The use of subscripts in references to the recordset yielded by a SORT RECORDS statement sometimes can produce unexpected results. If the BY EACH option appears in a SORT statement, records in which the BY EACH field is multiply occurring are copied several times (once for each field occurrence) to the system scratch file CCATEMP. In each copy, the occurrences of the BY EACH field are rotated, so that the occurrence used as the sort key appears first. Therefore, a subscripted reference to this field can yield different values for different copies of the record.