190 likes | 322 Views
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.
E N D
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.
EGL Arrays Like many other programming languages, EGL can group variables of the same type into arrays. EGL supports the following kinds of arrays: • Array Literals – fixed in size • Dynamic arrays • Structure field arrays – aka “record arrays” • General info on arrays: • Are 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 • 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 type Customers Customer[0]; //A dynamic array with no records smallStrArray string[4]; //an array of 4 strings orderArray Orders[8]; //An array of 8 order records
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 brackets Result int = myInts[3] + myInts[4]; i int = 3; Result int = myInts[i] + myInts[i+1]; tempMax int = premiumMax[i];
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=6 }; // An array of customer records, with no elements initially – that can hold up to 100 records CustomerArray Customer[0] {maxSize=100}; // An array of customerOrders records – which is a record type containing an embedded array of Orders records custOrdArray 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 CustomerOrders end
Manipulating Dynamic Arrays - EGL Array Functions To 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: • Declare a single instance variable for the array data • Declare an array variable (of the data type – that matches the single instance variable) • Assign values to the fields in the single instance variable • Issue an appendElement(dataOfSameType);statement.
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
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: custRecord i 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 basicRecord 01 CustInfo; 10 name; 20 firstName CHAR(20); 20 midInit CHAR(20); 20 lastName CHAR(20); 10 Address[3]; //Embedded array of (3) addresses within CustInfo 20 Street Char(20); 20 City Char(20); 20 State Char(2); 20 Zip Char(5); 10 Phone Char(14); end ***See slide Notes for additional details
EGL Structure Fields with Arrays – Examples and Declaration – 2 of 2 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. • … • 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 grades • End //end-record ***See slide Notes for additional details
Workshop EGL Arrays and Array Functions – 1 of 4 Using the EGL Editor: • Edit newaccount.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
Workshop EGL Arrays and Array Functions – 2 of 4 From Page Designer – Edit newaccount.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
Workshop EGL Arrays and Array Functions – 3 of 4 From Page Designer: • From Page Data: • Select 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)
Workshop EGL Arrays and Array Functions – 4 of 4 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.
OPTIONAL Workshop EGL DataTypes and Records – 1 of 3 If 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.jsp
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 - Use Content Assist 2. Declare variables 3. Code the Function Assignment statements – use mouse-based copy/paste freely 1. Declare a Record Note the different EGL data types 4. Press Ctrl/S ***See slide Notes to get copy/paste code for this OPTIONAL workshop
OPTIONAL Workshop EGL DataTypes and Records – 3 of 3 From Page Designer/Page Data • Drag intVar onto the page. Create an Input control • Drag the newRecord function onto the page • Drag dtRec onto the page. Create Output controls • Drag dtRecArray onto the page. Create Output controls • Run the page on the server – and verify the behavior of the function and the datatypes.
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.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/S to save generate
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 not necessary to do so for this example.
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.
Topic Summary • 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