User Guide > Using Content Extraction Language > Script Examples > Example Script 6: User Defined Functions, Arrays, and Loops
Was this helpful?
Example Script 6: User Defined Functions, Arrays, and Loops
This example uses a Fixed Format Daily Order Report as the Source input. The script ignores all but the detail records of the report. Each detail record is read, parsed into fields, checked to verify it is not a duplicate, and output through the Map Designer. To do this, the input fields are stored in an input array. The input array is used to compare the current record information against records that have already been read to determine if the current record is a duplicate. If the current record is not a duplicate, the record information is stored in the unique record storage array. This array is used in the accept statement to output the record information to the Map Designer. This means that duplicate records are not output since they are not stored in the unique record storage array.
The script uses a user-defined function to verify the current record is not a duplicate and stores it in the unique record storage array. Single and multidimensional arrays are used in the user-defined function. For and while loops are used when looping through the input fields and the previously read records. The output statement assigns field headings.
Output Source File
RUN DATE: 10/13/95 PAGE: 1
DATE: 10/16/95 DAILY ORDER REPORT TIME: 03:10 PM
AS OF: 10/13/95
ORDER CUSTOMER CUSTOMER PRODUCT
NUMBER ID NAME ORDERED QUANTITY PRICE
122 RW456G BORLAND DOS 5 125.00
123 UI346V MICROSOFT WINDOWS 25 1,125.00
124 OE591V IBM OS/2 54 2,356.00
124 OE591V IBM UNIX 10 580.00
125 NN903H DELL DOS 5 125.00
125 NN903H DELL DOS 5 125.00
126 FR751D TANDEM WINDOWS 10 580.00
------------
DAILY TOTAL: 5,691.00
=======
CXL SCRIPT
 
#!djrr
# Fixed-Format Daily Order Report Script with examples of:
# * user defined functions
# * storage of field values into arrays
# * similating a multidimensional array
# * for and while loops
# * checking for duplicate records in input
#
BEGIN {
# set the field separator to a space
IFS = " ";
# recCount is used to hold the current number of unique
# records that have been read and stored in the inputArray
recCount = 0;
# this array is used to hold all or the unique record
# information so a check can be made for duplicates
# it is initialized here so that it will be global
storeArray[recCount, "0"] = "0";
}
# FUNCTION DECLARATIONS
######################## function verifyInput #########################
# verifyInput - This function loops through all of the fields on the
# current line, stores them in an inputArray, verifies the record in the inputArray is not a duplicate in the storeArray, copies uniques record information into the storeArray.
###################################################################
function verifyInput() {
i = 0;
totalFields = NF;
#Store the input fields in the inputArray for verification
while (NF) {
i += 1;
#store the field value in inputArray subscripted by the field name
#note: 1, 2, 3, ... are being used as field names
inputArray[i] = $1;
#shift moves the $n fields one to the left...$1 becomes $2...
#shift also subtracts 1 from NF each time it is called
shift;
}
# Loop through the entries in the storeArray and compare with the input fields to verify that the input record is not a duplicate
unique = 1; j = 1;
while (j <= recCount) {
dupFields = -1;
#loop through each entry in the inputArray and check if any fields
#are equal to any fields in the storeArray if so increment dupFields
for (currField in inputArray) {
if (inputArray[currField] == storeArray[j, currField]) {
dupFields += 1;
}
}
j += 1;
#incrementing j takes us to the next record in storeArray
#if all the fields in a record are duplicate then the record is not unique
if (dupFields == totalFields)
unique = 0; # duplicate record
}
#If we have a unique record
if (unique) {
recCount += 1; #increment the record count
#Store unique records in storeArray
for (currField in inputArray) {
#store the values from the inputArray to the storeArray
storeArray[recCount, currField] = inputArray[currField];
}
}
return unique; #return to the caller whether the record was unique or not
}
# Reject all empty lines and non-detail lines in printout
{ $0 = rtrim($0);
if (length($0) == 0)
REJECT;
}
# These pattern-action statements handle headings, totals and line separators
/----------/ { REJECT; }
NF < 3 { REJECT; }
/TOTAL:/ { REJECT; }
/^ORDER/ { REJECT; }
/^NUMBER/ { REJECT; }
/^... DATE/ { REJECT; }
/^DATE/ { REJECT; }
ltrim($0) ~ /^AS OF/ { REJECT; }
# If we get to here, it's a detail line... Notice there is no pattern so these action statements are performed on every line that isn't eliminated prior to getting here.
{
#Call the verifyInput user defined function to verify input fields
#are uniques and if so store them in storeArray
unique = verifyInput();
#If input record was not unique break and go to the next record
if (0 == unique)
BREAK;
#Have a unique record if get here so output to the Map Designer
accept <"Order Number"> storeArray[recCount,1],
<"Customer Id"> storeArray[recCount,2],
<"Customer Name"> storeArray[recCount,3],
<"Product Ordered"> storeArray[recCount,4],
<"Quantity"> storeArray[recCount,5],
<"Price"> storeArray[recCount,6];
}
Output Records
 
 
Order Number
Customer Id
Customer Name
Product Ordered
Quantity
Price
1
122
RW456G
BORLAND
DOS
5
125.00
2
123
UI346V
MICROSOFT
WINDOWS
25
1,125.00
3
124
OE591V
IBM
OS/2
54
2,356.00
4
124
OE591V
IBM
UNIX
10
580.00
5
125
NN903H
DELL
DOS
5
125.00
6
126
FR751D
TANDEM
WINDOWS
10
580.00
 
Last modified date: 02/09/2024