EGL Programming – Data Parts and Assignment Statements – 4 – Arrays
description
Transcript of EGL Programming – Data Parts and Assignment Statements – 4 – Arrays
®
IBM Software Group
© 2006 IBM Corporation
EGL Programming – Data Parts and Assignment Statements – 4 – Arrays
These slides walk you through the terms and concepts around declaring EGL Arrays to organize repeating records and primitives. It also covers the EGL built-in Array handling functions and manipulating array information.
2
EGL Arrays Like many other programming languages, EGL can group variables of the same type into
arrays. EGL supports the following kinds of arrays: 1. Array Literals – fixed in size2. Dynamic arrays3. Structure field arrays – aka “record arrays”
General info on arrays: Are one(1)-based in subscriptsAre one(1)-based in subscripts - (like COBOL and RPG) - not zero-based
The value within the brackets must be: > 0 and < the current array limit> 0 and < the current array limit Can have multiple dimensions – up to seven levels Size is initialized with an integer value within brackets next to its type
Products productRec[6]; //A dynamic array – with six records initialized Note that you can only refer to an array element that has been initialized Products[0].Name = “Smith” run-time error. Products[7].name = “Smith” run-time error.
Can be of any Primitive Data type or a defined Record typeCustomers Customer[0]; //A dynamic array with no records smallStrArray string[4]; //an array of 4 stringsorderArray Orders[8]; //An array of 8 order records
3
Array Literals An array literal consists of a pair of brackets that contains a comma separated list of literals
(including other array literals) or expressions (including array variables).
Each array literal has a type, and can be used anywhere an array of the given type is allowed—for instance, as an initializer for an array variable. Here are some examples:
myInts int[5] = [1,2,3,4,5]; //initialize when you declare!
validStates char(2)[6] = ["NY","NJ","NC","DE","MD","WA"];
premiumMax decimal(9,2)[4] = [99999.99,59899.43, 92342.02, 9843233.01];
Refer to an array element using an integer value or integer variable subscript between square bracketsResult int = myInts[3] + myInts[4];i int = 3;Result int = myInts[i] + myInts[i+1];tempMax int = premiumMax[i];
4
Dynamic Arrays The syntax for declaring a dynamic array is shown in the following examples:
// An array of 6 elements or less, with 4 elements initially myDataItemArray myDataItem[4] { maxSize=6maxSize=6 };
// An array of customer records, with no elements initially – that can hold up to 100 recordsCustomerArray Customer[0] {maxSize=100};
// An array of customerOrders records – which is a record type containing an embedded array of Orders recordscustOrdArray CustomerOrders[0]; //No initialized elements
record CustomerOrders type basicRecord CustomerId CustomerId; FirstName FirstName; LastName LastName; Orders Orders[0]; //Array of Orders records as a field in CustomerOrdersend
5
Manipulating Dynamic Arrays - EGL Array FunctionsTo add rows to a Dynamic Array use the appendElement() array function
arrayVar.appendElement(<VariableOfArrayType>);
… where the appended data value is a variable of the same type as the array. Primitive Record type
So – to explicitly add new rows to a dynamic array:1. Declare a single instance variable for the array data2. Declare an array variable (of the data type – that matches the single instance variable)
3. Assign values to the fields in the single instance variable4. Issue an appendElement(dataOfSameType); statement.
6
Manipulating Dynamic Arrays - Other EGL Array Functions Size(<ArrayVariableName>)
yields an integer with the number of rows in the array: sz int = size(custArray);
ArrayVariableName.insertElement(<arrayType>, idx); inserts to specific row location in the array – existing rows bumped up by one custArray.InsertElement(customerRec, 2);
ArrayVariableName.removeAll(); zeros (empties) array of all rows in the array: custArray.removeAll();
ArrayVariableName.removeElement(idx); removes a specific row from the array: custArray.removeElement(4);
Use Content Assist to code construct your statements
7
EGL Structure Fields with Arrays – Examples and Declaration – 1 of 2 You can embed an array of fields into a structure Record. Reference the array portion as: recName.groupFieldName[n].<fieldname>…custRec custRecord; //Variable Declaration – single structure record, of type: custRecordi int; //integer subscript for referencing array element…Function main()
i = 3;custRec.CustInfo.Address[i].City = "Nyack"; //Variable.groupVar.arrayVar[idx].field = value
…record custRecord type basicRecord01 CustInfo;
10 name; 20 firstName CHAR(20); 20 midInit CHAR(20); 20 lastName CHAR(20); 10 Address[3]; //Embedded array of (3) addresses within CustInfo20 Street Char(20);20 City Char(20);20 State Char(2);20 Zip Char(5);10 Phone Char(14);
end
***See slide Notes ***See slide Notes for additional for additional detailsdetails
8
… myClass StudentYTD; //Record variable of type Student…Function main() myClass.class.names[3].student = "Smith"; //Assign 3rd student name: Smith myClass.class.names[3].grades.grade[1] = 95.0; //Assign 1st test score to Smith
…End //end logic-part
record StudentYTD type basicRecord 01 class;
10 classtitle char(20);10 Instructor char(20);10 names [8]; //Array of eight students 20 student CHAR(10); 20 grades GradeRec; //Grades – variable of type: GradeRec
End //end-record
record GradeRec type basicRecord 01 grade Decimal(4,2) [20] ; //20 occurrence array of gradesEnd //end-record
You can also have embedded arrays (see below). For structured arrays the subscript is still at the end. The first value references the outer array, the second value the inner array.
Example: Declare a record for student information. For a class of eight students, maintain an array of 20 grades for each student.
EGL Structure Fields with Arrays – Examples and Declaration – 2 of 2
***See slide Notes ***See slide Notes for additional for additional detailsdetails
9
Workshop EGL Arrays and Array Functions – 1 of 4
Using the EGL Editor:Using the EGL Editor:
Edit newaccount.eglnewaccount.egl
Add The accounts array variable An integer variable The six functions shown here
Note: Use Content Assist to help you create the Array Function statements
Press Ctrl/S and clean up all syntax errors
Note that //comments are optional
10
Workshop EGL Arrays and Array Functions – 2 of 4From Page Designer – From Page Designer – Edit Edit newaccount.jspnewaccount.jsp
1. Select and delete the existing Submit Button
2. From Page Data – select all of the JSF Handler functions.
3. Drag and Drop them onto the page – where they will create seven new submit buttons
4. Again from Page Data – select the selRow – int field, and drag and drop it onto the page, below your new buttons From Configure Data Controls, specify this field as:
Updating an existing row
11
Workshop EGL Arrays and Array Functions – 3 of 4From Page Designer: From Page Designer: From Page Data:
Select accounts – accountRec[]accounts – accountRec[] Drag and Drop it onto the page below
SelRow
From Configure Data Controls, Create this control for:
Displaying an existing record (read-only)
12
Workshop EGL Arrays and Array Functions – 4 of 4From Page Designer:From Page Designer:
Run the page Enter valid data, and press
addAccount Create several rows in the
array Utilize the remaining
functions to: Delete a specific row Insert a new row at a
specific location in the array Select (retrieve) a row from
the array into the detail account fields
Update a specific row in the array
Clear the array Etc.
13
OPTIONAL Workshop EGL DataTypes and Records – 1 of 3If time permits, create a .JSP page and JSFHandler that use most of the common EGL
variable datatypes and records found in business applications (standalone variables, single record, record array).
Start by creating a new web page named: eglDataTypePage.jspeglDataTypePage.jsp
14
OPTIONAL Workshop EGL DataTypes and Records – 2 of 3 Edit the EGL Code for
the page – and add the following: - Do the steps in order- Do the steps in order - Use Content Assist- Use Content Assist
2.2. Declare variables Declare variables
3.3. Code the Function Code the FunctionAssignment statements – use mouse-based copy/paste freely
1.1. Declare a Record Declare a Record
Note the different EGL data types
4.4. Press Ctrl/S Press Ctrl/S******See slide NotesSee slide Notes to get copy/paste to get copy/paste
code for this OPTIONAL workshopcode for this OPTIONAL workshop
15
OPTIONAL Workshop EGL DataTypes and Records – 3 of 3From Page Designer/Page Data
1.1. Drag Drag intVarintVar onto the onto the page. Create an page. Create an Input Input controlcontrol
2.2. Drag the Drag the newRecordnewRecord function onto the pagefunction onto the page
3.3. Drag Drag dtRecdtRec onto the onto the page. Create Output page. Create Output controls controls
4.4. Drag Drag dtRecArraydtRecArray onto onto the page. Create Output the page. Create Output controlscontrols
5.5. Run the pageRun the page on the on the server – and verify the server – and verify the behavior of the function behavior of the function and the datatypes.and the datatypes.
16
OPTIONAL WORKSHOP - Nested Arrays – 1 of 3 As shown in this section, you can embed a dynamic array, as a variable inside of another dynamic array record – down as many levels as seven - for your business logic requirements. This can be very useful, in presenting
related/“normalized” table information – as this example shows: From Project Explorer, create a new Web Page
Right-click over \WebContent\ - select; New > Web Page Name the page: treePage.jsptreePage.jsp - and select a template. Change the header text (as shown) (Right-click, and) Edit the page code Replace the existing JSFHandler, with Copy/Paste the code from this slide’s Notes Check out the embedded (nested) Dynamic Arrays …which are defined at the bottom of the code
Press Ctrl/SPress Ctrl/S to save generate
17
OPTIONAL WORKSHOP - Nested Arrays – 2 of 3 From Page Designer – from Page Data, Select myCompany and drag/drop it onto the Content
Area. For each embedded array inside the myCompany variable, you will seen a Multi-Column Data ellipsis, allowing you to customize the control type, for each embedded (nested) array to be displayed through this JSF dataTable.
Note that in this example, we’ve added several columns to categorize the table rows (you did this on allorders2.jsp, remember?) It’s notnot necessary to do so for this example.
18
OPTIONAL WORKSHOP - Nested Arrays – 3 of 3 Run the page on the Server. Note the embedded (nested array) information, displayed
hierarchically in the dataTable Note again, that we are using alternate row colors, dataTable borders, and row categorization
to obtain this functionality and rendering.
19
Now that you have completed this topic, you should be able to:
Define the different types of EGL Arrays Declare array variables Assign values to reference-able array elements Use the Array built-in-functions to:
Add array elements Delete array elements Clear an array Insert an array element at a particular array row
Topic Summary