Transcript Slide 1

IBM Software Group

EGL Programming – Assignment Statements and Math Operations

These slides walk you through the terms and concepts around using EGL to assign variables and set their values, as well as doing concatenation and mathematical operations.

® © 2006 IBM Corporation

EGL Assignment Statements

 There are three types of EGL assignment statements: 1. Right-to-left assignment: ReceivingVar = SendingValue; //  value moved from right to left

PhoneNbrOut = PhoneNbrInput; empRecout.LastName = empRecIn.LastName;

2. Left-to-right (Move): Move SendingValue to ReceivingVar; //value moved from  left to right, on a byte by byte basis

move PhoneNbrInput to PhoneNbrOut; move empRecIn.LastName to empRecOut.LastName;

3. Initial/Empty “

set

” statements: You can blank out a record or field, re-initialize a record or field using

set set PhoneRecord initial; set empRecIn empty;

While it is may seem easier to code right-to-left assignment statements, there are several useful and time-saving variations on

move

provided in EGL available as statement templates. 2

EGL Assignment – Move byName , Move byPosition

 Structure-based moves –

move

statement extensions for handling records 

move byName

 When you specify: Move A to B

byName

, data is written from each field in the source to a same-named field in the target.

move byPosition

 The purpose of: Move A to B

byPosition

is to copy data from each field in one structure to the field at the equivalent position in another – irrespective of field names .

** Array-specific move statement extensions.

You can move all or some of the rows of “like-typed” arrays using: move for all , or move for count 

Move for all -

element array.

The purpose of

for all

is to assign values to all elements in a target fixed 

Move for count -

The purpose of for count is to assign values to a sequential subset of elements in a target fixed-element array.

Recommendation

– always use Content Assist to help you code Move statements!

** NOTE ** for all

and

for count

work only with arrays that are declared with an initial value

> 0: arrayRec type[n]; //where n is a number > 0

3

EGL Mathematical Operators

 EGL supports a complete lexicon of math – allowing you to express business calculations and rules of any scale and computational complexity.

 Besides the math operators shown above, EGL also includes a comprehensive set of math library functions and operations that will be discussed in a later unit of this course.

4

EGL Mathematical Operators

 EGL supports a complete lexicon of math – allowing you to express business calculations and rules of any scale and computational complexity.

 Besides the math operators shown above, EGL also includes a comprehensive set of math library functions and operations that will be discussed in a later unit of this course.

5

EGL Logical Operators

 EGL supports a large number of simple and advanced Logical Operators – allowing you to express business rules of any depth and sophistication.

6

Redefining (casting) Data – Using AS

You concatenate strings (and char fields) in EGL using the + (plus) sign

fldOut1 = strfld1 + strfld2; //concatenate fields

 You can use this same technique to redefine or “cast” numbers and dates, times, etc. to strings by simply adding a ""+ to the beginning of the expression

fldOut1 =

""+

numField + strField; //concatenate 1st a numeric field

Alternatively, you can use the

AS

operator, to redefine a field to another field dynamically within a function (see Content Assist options)  //Concatenate a numeric field as if it were a string

strFldOut = numField as string + strField;

 //Perform math on a string field, as if it is a numeric

numFldOut = strField as int + numField;

7

Sub-stringing EGL Variables

You can substring a string or char text field with the following expression:

[startByte:endByte];

...where startByte is an integer >= 1, that specifies what position in the string to begin the substring operation, and endByte (an integer <= the length of the field) specifies at what position to end the substring operation. Note that the substring can be the left or right part of an assignment or conditional statement (see examples):

strField[7:9]; //Reference bytes 7, 8 and 9 of the variable strField

 Examples – note, you can use either a literal integer or an integer variable as start/endBytes.

 Make fldOut4 = to the 4th through 7th character positions of fld1.

sub1 = 4; sub2 = 7; fldOut4 = fld1[sub1:sub2];

Right-justify

a string variable(strField) value, in a char (fixed-length) field of 21 bytes

charField [21 - strlib.characterLen(strField):20] = strField;

 Standard Y2K date-windowing check – using date field stored as a string in format “yymmdd

” if ( dateAsStringField [1:2] > “50” ) //1900’s date processing else //2000’s date processing

8

*** See Notes: - For several additional useful sub-string code examples!

Enhanced Concatenation and Mathematical (Shortcut) Operators

You can add numeric – or concatenate string variables using the following syntax:

myVariable += someOtherVariable;

…this is equivalent to the traditional:

myVariable = myVariable + someOtherVariable;

 The “

+=

” operator will work with  strings (concatenation)  numerical values (mathematical addition).

 The following operations are only supported with numerical values: -= (subtract)

a -= 4;

// a = a - 4; *= /= (multiply) (divide)

a *= 4;

// a = a * 4;

a /= 4;

// a = a / 4; 9

EGL Constants – 1 of 2

 Like many contemporary programming languages, EGL allows you to declare variables as

constants

– variables with values that are initialized once, and cannot be changed. Using constants will allow you to make your programs more read-able, and can make them easier to maintain over time.

To declare a variable as a constant, type the following: // Reserved Variable Primitive Constant // word name type value

const = value;

Examples:

const

MAXSAL

decimal const const const

MINSAL MINAGE MAXAGE

decimal int int

(7,2) = 100000.00; (7,2) = 20000.00; = 18; = 99; 10

EGL Constants – 2 of 2

 Another use for EGL constants might be to translate COBOL 88-level variables into EGL.

Here’s a COBOL structure and program snippet: WORKING STORAGE.

01 COLOR PIC X(1) VALUE 'Y'.

88 YELLOW VALUE 'Y'.

88 BLUE VALUE 'B'.

88 RED VALUE 'R'.

And here’s an equivalent EGL re-engineered snippet: color char(1); const YELLOW char(1) = "Y"; const BLUE char(1) = "B" ; const RED char(1) = "R"; PROCEDURE DIVISION.

PARA-1.

SET RED TO TRUE.

IF YELLOW DISPLAY 'COLOR IS YELLOW' IF BLUE DISPLAY 'COLOR IS BLUE' IF RED DISPLAY 'COLOR IS RED' EXIT PROGRAM. function main() color = RED; case (color) when(YELLOW) writeStdOut(“Color is Yellow"); when(RED) writeStdOut("Color is Red"); when(BLUE) writeStdOut("Color is Blue"); end //end-case end //end-main

11

  •

Resolving Syntax Errors – Best Practice

 EGL syntax errors are usually very easy to resolve – because: The

Problems

on the error to view allows you to click position your cursor on the problem line – or even problem language element. You can mouse over the Red circle’d X and see each error, Best Practice: From either the

Results Problems EGL Generation

or the view – find and fix all

IWN.SYN

errors before

IWN.VAL

errors.

12

 

Workshop – Assignment Statements and Math Operators – 1 of 2

Using the math operators and assignment statements you have just learned, create the following web page calculator – named calculator.jsp. Use the A_gray.htpl Sample Template.

 Note that if you started this workshop earlier (i.e. calc.jsp) you can use what you’ve already completed, and fill out the remaining functions.

Content 13

   

Workshop Steps – 2 of 2

Create the calculator.JSP Page

– and modify the page header as shown on the previous slide.

From the JSFHandler

(i.e. from

calculator

.

EGL

) – add code following these steps:  

Create a basicRecord out of the three Page Data variables

Decimal

data type for all  Name them

Top, Bottom, Result

 Use the

sign

and

zeroFormat

properties on the

Result

variable

Add a function (below

Function

onPageLoad )

– which adds

Top

Result = Top + Bottom;

to

Bottom

, giving

Result

Ctrl/S

– save and syntax check your function 

Copy and paste the function 4 times in your JSFHandler

– and modify the remaining calculations for subtraction, multiplication and division

Ctrl/S

– save when finished with your EGL statements

From Page Designer/Page Data view

(editing

calculator.JSP

) 

Drag the three variables onto the Content Area

– creating controls that update an existing record 

Top

and

Bottom

should be Input fields with message tags, 

Result

should be an Output field  

Drag each of the functions onto the Content Area

 From the Properties/Format view – re-Label the Submit Buttons

Run on server

– test and verify your work 14

Workshop – Batch Program Debugging – 1 of 7

Create a batch program and debug it! Steps: 1. Create a new EGL main program in the

\programs\

directory • Name the program:

payrollProgram

2. From the Notes section of these slides – select all of the Program Source, and copy it to your paste buffer. 3. Select and delete (replace) the boiler plate source for payrollProgram.egl this slide – with the source from the notes section of 4. Save the file (

Ctrl/S

) 5. Review the payrollProgram source. the EGL functions, statements and – and enhance – for a number of  Read the comments and browse through records. This program will be one we use workshops over the next few days, so be sure you’re comfortable what it’s doing.

15

Workshop – RBD/EGL Debugger – “101”

 The EGL Debugger is very powerful, and we’ll see over the course of the next few workshops how to use more of its features. Let’s go over a few quick setup concepts, before we start our first session: 1.

Set preferences for the Debugger to Stop at first line. While you will probably always remember to set break points before you launch a Debugging session if you don’t remember, this preference will.

1.

2.

From Windows, Preferences, expand EGL and expand Debug Check these options (see ***Notes for details)

2.

Variable List – allows you to see and set (and re-set) variables dynamically during your debug session 1.

To reset variable values:

1. Single-click the variable value. 2. Change the value. 3. Click anywhere else

16

Debug payrollProgram.egl – 2 of 7

Return to your EGL program source: 1.

Create “break-points” in your code to tell the Debugger where to stop  Add Breakpoints 

Double-click in the gray border area of the EGL Editor, next to the line you want to stop at

 2. After you have set all of your break-points, from Project Explorer, •

Right-click over payrollProgram.egl

• Select:

Debug EGL Program Setting Break Points

You will be prompted to Confirm Perspective Switch Do so  The next slide shows the Debug Perspective. Note the various Sections and Views.

17

 

Workshop – Batch Program Debugging – 3 of 7

The source code debugger allows you to see your EGL statements execute – in real-time, and view variable values. There are a number of sub-views

1. Action Bar

1. Action Bar

statement – allows you to click an icon and step (execute) the current (highlighted) 

2. Variables view

– allows you to see (or double-click and modify) program variable values 

3. Your source

(highlighted) in the Content Area, shows which line is the “next sequential statement” 

4. The Outline view

shows your functions, records, fields etc. and allows you to navigate to a source element quickly

3. Your EGL Program Source

18

2. Variable Values 4. Outline View

   

Workshop – Batch Program Debugging – 4 of 7

From the

Action Bar

instruction – click the

Step Into

icon - to execute the current From the

Variables

view, expand some of the folders to see: record, group and field level contents change You can also Click the run icon - to execute all of the statements between breakpoints 19

   

Workshop – Batch Program Debugging – Context Menu – 5 of 7

From within the Content Area, try the following: Right-click in the gray side-border of the code.

Try out the following options:   

Show Line Numbers

Add Breakpoint (on the fly) Run to Line

Toggle this on/off From the current instruction, execute all instructions to the line your mouse-cursor is on

 

Jump to Line:

From the current instruction, branch immediately to the instruction on the line your mouse-cursor is on

This can be used to Restart your debugging session, although it does NOT re-initialize variable values Add Task or Bookmark

Allows you to create a list of tasks and bookmarks that you can:

See in the EGL editor

See in an organized list by opening the Tasks View or Bookmarks View

Note that there’s a Context Menu for Variables too!

20

  

Workshop – Batch Program Debugging – 6 of 7

Step through this program - seeing how the various move statements work – seeing the variable values – change Try modifying a variable value:  From the Variables view  Right-click over a field  Select Change Value…  (modify it)  Note that earlier we described a different way for you to change variable values dynamically – by clicking on the actual value, overtyping it, and clicking anywhere else in the Workbench (to save your change) 21

 

Workshop – Batch Program Debugging – 7 of 7

When you have finished debugging – end-of-program) (whether or not you’ve debugged all the way to 1. Terminate your session  Click the red box  2. Right-click over the Debug statements  Select: Remove all terminated 3. Return to the Web Perspective  Right-click over the Debug icon (top right hand corner of the workbench)  Select Close 22

Topic Summary

 Now that you have completed this topic, you should be able to:  List the three different types of EGL assignment operations  Describe three of the EGL Move statement extensions  Define the majority of the math operators, and use them in business logic coding  Code essential business math (add, subtract, multiply and divide)  Debug batch programs, and watch  Executing EGL statements  Changing EGL variables 23