You are on page 1of 42

Unit 4.

Manipulating Data in Calculations

Copyright IBM Corp. 1996, 2000

4-1

Unit Objectives - Data Manipulation


After completing this unit, you should be able to:
code calculation specifications using free form operation codes, and

expressions for: assignment of numeric, character, and logical values arithmetic operations logical operations string handling operations
OL864000

Code Built In Functions in expressions


Copyright IBM Corp. 1996, 2000 4-2

Types of Expressions
Assignment / Data conversion Arithmetic Logical String
OL864005

Copyright IBM Corp. 1996, 2000 4-3

Assignment Opcodes
Eval

Assignment via simple expression Result = Expression Supports all Data Types Data types must be compatible Left justified with character operands

EvalR - same as Eval but:

Character only Right justified


Move
Copyright IBM Corp. 1996, 2000 4-4

Right justified
MoveL

Left justified
MoveA

For arrays
OL864105

Numeric Assignment
Assign Value CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval NumericVar = 567 ** C Eval Num2 = 0 - Num2 Clear Numeric to Zero CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq ** C Eval NumericVar = 0 C Eval NumericVar = *Zeros
OL864110

Copyright IBM Corp. 1996, 2000 4-5

Assignment of Character Variable


Assign Value to Character Variable CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval CharVar = A_String C EvalR CharVar = A_String CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C MoveL (P) A_String CharVar C Move (P) A_String CharVar Assign Blanks to Character Variable CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval CharVar = C Eval CharVar = *Blanks C EvalR CharVar = *Blanks ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++ C MoveL *Blanks CharVar C Move *Blanks CharVar
OL864115

Copyright IBM Corp. 1996, 2000 4-6

Move
Character to Character
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Char3 S 3 Inz( NOP ) D Char6 S 6 Inz( ABCDEF ) CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq ** C Move Char3 Char6 ** Char6 = ABCNOP

Numeric to Character
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Num3 S 3 0 Inz(123-) D Char6 S 6 Inz( ABCDEF ) CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq ** C Move Num3 Char6 ** Char6 = ABC12L

Character to Numeric
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Char3 S 3 Inz( NOP ) D Num5 S 5 0 Inz(12345) CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq ** C Move Char3 Num5 ** Num5 = 12567OL864120

Copyright IBM Corp. 1996, 2000

4-7

MoveL
Numeric to Character
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Num3 S 3 0 Inz(123-) D Char6 S 6 Inz( ABCDEF ) CL0N01Factor1+++++++Opcode++++Factor2+++++++Result++++++++Len++D+HiLoEq ** C MoveL Num3 Char6 ** Char6 = 12LDEF

Character to Numeric (Long)


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Char3 S 3 Inz( NOP ) D Num5 S 5 0 Inz(12345) CL0N01Factor1+++++++Opcode++++Factor2+++++++Result++++++++Len++D+HiLoEq ** C MoveL Char3 Num5 ** Num5 = 56745

Character to Numeric (Short)


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Char3 S 3 Inz( NOP ) D Num2 S 2 0 Inz(12) CL0N01Factor1+++++++Opcode++++Factor2+++++++Result++++++++Len++D+HiLoEq C MoveL Char3 Num2 ** Num2 = 56OL864125

Copyright IBM Corp. 1996, 2000

4-8

Rules of Move
Move/MoveL/MoveA controlled by length of shorter field length Data conversion is automatic Result field cleared first by P extender Decimal Alignment not performed
OL864130

Copyright IBM Corp. 1996, 2000 4-9

Copyright IBM Corp. 1996, 2000

4-10

OL864200

Arithmetic Opcodes
Legacy OpCodes:

Copyright IBM Corp. 1996, 2000 4-11

ADD SUB MULT DIV MVR XFOOT SQRT Z-ADD Z-SUB

Best coded using EVALs and BIFs

OL864205

Addition and Subtraction


CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval YTDPay = YTDPay + GrsPay ** C Eval NetPay = Netpay - Taxes ** C Eval NumericVar = Var1 - Var2 + 3 + Const1
OL864210

Copyright IBM Corp. 1996, 2000 4-12

Multiplication
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Result1 S 5P 1 D Result2 S 5P 1 D Result3 S 5P 2 ** CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++ C Eval Result1 = 123.456 * 10 ** Result1 = 1234.5 - truncated C Eval (H) Result2 = 123.456 * 10 ** Result2 = 1234.6 - rounded C 123.456 Eval (H) Result3= 10 * 123.466 ** Result3 = 234.56 - numeric overflow
OL864215

Copyright IBM Corp. 1996, 2000 4-13

Numeric Overflow
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D NumericVar S 7P 2 D Fld1 S 3 0 INZ(993) D Fld2 S 3 0 INZ(50) D Fld3 S 4 1 D Fld4 S 5 1 ** CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++ C Eval Fld3 = Fld1 + Fld2 C Eval Fld4 = Fld1 * Fld2
Copyright IBM Corp. 1996, 2000 4-14

What will be the values of Fld3 and Fld4? What will happen when these statements are executed?
OL864220

Division and Remainders


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D NumericVar S 5P 0 D Remainder S 5P 0 D Months S 3 0 INZ(54) D Years S 3 0 INZ D RemMon S 2 0 INZ CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq *************************************************************** C Eval NumericVar = 200 / 3 ** ** NumericVar = 66 *************************************************************** C Eval (H) NumericVar = 200 / 3 ** ** NumericVar = 67 *************************************************************** C Eval NumericVar = %div(200:3) C Eval Remainder = %rem(200:3) ** NumericVar = 66 ** Remainder = 2 *************************************************************** C Eval Years = Months / 12 C Eval RemMon = %rem(Months : 12) ** Years = 4 ** PartMon = 6
OL864225

Copyright IBM Corp. 1996, 2000 4-15

Exponentiation

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++ D CubeRoot S 5 3 D Cubed S 5 3 ** D Number S 4S 0 Inz(144) D SqRoot S 2S 0 ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Eval (H) Cubed = 3**3 C Eval CubeRoot = Cubed**(1/3) ** CubeRoot = 2.99 C Eval (H) CubeRoot = Cubed**(1/3) ** CubeRoot = 3 C Eval SqRoot = Number**.5 ** SqRoot = 12

Copyright IBM Corp. 1996, 2000 4-16

OL864230

Copyright IBM Corp. 1996, 2000

4-17

OL86410L

Comparison Operations
x = y x <> y x > y x >= y x < y x <= y
Copyright IBM Corp. 1996, 2000 4-18

Equality Not equal Greater Than Greater Than or Equal Less Than Less Than or Equal Logical and Logical or Logical not
OL864305

x AND y x OR y x NOT y

Class Exercise
CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ ** 1 C Eval *In99 = *ON ** 2 C Eval *In99 = (X > Y) ** 3 C Eval *In99 = (X > Y) AND (Z <> 0) ** 4 C Eval *In99 = ((X > Y) AND (Z <> 0)) OR *In23 ** 5 C Eval *In99 = (X > Y) OR NOT *in23
Copyright IBM Corp. 1996, 2000 4-19

What is the meaning of each expression?

OL864310

Setting Indicators
CN0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Eval *In99 = *ON C Eval *In(99) = *ON CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Move *ON *In99 ** ** C MoveA 101 *In(97) ** C Eval *In97 = *On C Eval *In98 = *off C Eval *In(99) = *ON
OL864315

Copyright IBM Corp. 1996, 2000

4-20

If / Else / EndIf

CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C If Fld1 = Fld2 C Eval *In99 = *ON C Else C Eval FldTot = FldTot + Fld1 C Endif

C C C C ** C : C

Eval If Eval Endif If : If

*In99 = (Fld1 = Fld2) NOT *In99 Fldtot = Fldtot + Fld1

Copyright IBM Corp. 1996, 2000 4-21

*In10 : NOT *In10


OL864320

IF Expressions
CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C If *in99 C C Else C C C C C C
Copyright IBM Corp. 1996, 2000 4-22

| | | | | |

If Else | If | | Else

(X < Y)

(X > Y) AND (Z <> 0)

C C C C C C C

| | | If | | | | | | | | | EndIf | | EndIf | EndIf EndIf

((X = Y) AND (Z < 0)) OR *in23

Ol864325

Copyright IBM Corp. 1996, 2000

4-23

OL864400

String Handling BIFs


BIF %CHAR %EDITC %EDITW Purpose Convert to character Edit numeric using edit code Edit numeric using edit word

%REPLACE Replaces all or part of character string %SCAN %SUBST


Copyright IBM Corp. 1996, 2000 4-24

Matches search argument against character string Extracts substring from a character string Removes left and right blanks from a string Removes left blanks from a string Removes right blanks from a string
OL864405

%TRIM %TRIML %TRIMR

Concatenation
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++ D FirstName S 6 INZ( John ) D LastName S 6 INZ( Smith ) D FullName S 15 INZ CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Eval FullName = FirstName + LastName ** FullName = John Smith C Eval FullName = FirstName + + LastName ** FullName = John Smith C Eval FullName = FirstName + + LastName ** FullName = John Smith
Copyright IBM Corp. 1996, 2000 4-25
OL864410

Remove Blanks - %TRIM


%TRIM(string) %TRIML(string) %TRIMR(string)
%TRIM strips leading and trailing blanks from character or graphic fields %TRIML strips leading blanks only %TRIMR strips trailing blanks only

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D AString S 17 Inz( This is it ) D Another S 12 Inz D YetAnoter S 20 Inz D BigOne S 26 Inz D OneMore S 10 Inz CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval Another = %TRIML(AString) ** Another = This is it C Eval YetAnother = %TRIMR(AString) + ? ? ** YetAnother = This is it?? C Eval OneMore = %TRIM(AString) ** OneMore = This is it C Eval BigOne = %TRIMR(AString) + %TRIML(AString) ** BigOne = This is itThis is it

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++ D FirstName S 6 INZ( John ) D LastName S 6 INZ( Smith ) D FullName S 15 INZ CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Eval FullName = %trimr(LastName) C + , C + %trimr(FirstName) ** FullName = Smith, John
OL864415

Copyright IBM Corp. 1996, 2000

4-26

Determine the Length of a String - %Len


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Length S 2 0 Inz D String S 25 Inz( Learning Services

CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Eval Length = %Len(String) ** Length = 25 C Eval Length = %Len(%TrimR(String)) ** Length = 17


Copyright IBM Corp. 1996, 2000 4-27

OL864420

Scan for Character String - %Scan (1/2)


%SCAN(search argument : source string {: start})

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Position S 2 0 Inz D String1 S 7 Inz( ABCDEFG ) D String2 S 2 Inz( RS ) D String3 S 5 Inz( TUVXY ) D String4 S 25 Inz( Learn ) CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq ** C Eval Position = %scan( CD : String1) ** Position = 3 C Eval Position = %scan(String2 : String3 : 3) ** Position = 0 C If %scan(%trim(String4) : String3) **

Copyright IBM Corp. 1996, 2000 4-28

OL864425

Scan for Character String - %Scan (2/2)


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Source S 15A Inz( Dr. Doolittle ) D Pos S 5 0 Inz CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval pos = %scan( oo : source) ** 1 Pos = 6 C Eval pos = %scan( D : source : 2) ** 2 Pos = 5 C Eval pos = %scan( abc : source) ** 3 Pos = 0 C Eval pos = %scan( Dr. : source : 2) ** 4 Pos = 0
OL864430

Copyright IBM Corp. 1996, 2000 4-29

Extract Substring - %Subst


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Comment S 16 INZ( It s a great day ) D NameField S 20 INZ( John Smith ) D FirstName S 10 INZ D ReverseNm S 20 INZ D BlankPos S 2 0 INZ CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval %SUBST(Comment:8:5) = lousy ** The field Comment = It s a lousy day C Eval BlankPos = %scan( : NameField) C Eval FirstName = %SUBST(NameField : 1 : C BlankPos -1) ** FirstName = John C Eval ReverseNm = %TrimR(%Subst(NameField : C BlankPos + 1)) C + , C + %Subst(NameField : 1 : C BlankPos - 1) ** ReverseNm = Smith, John
OL864435

Copyright IBM Corp. 1996, 2000 4-30

Convert to Character - %Char


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D PostalCode S 5 0 Inz(30101) D PostExtend S 4 0 Inz(3490) D FullPostCode S 10 Inz ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C Eval FullPostCode = %char(PostalCode) C + C + %char(PostExtend)

OL864440

Copyright IBM Corp. 1996, 2000

4-31

Edit with %EditC / %EditW


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D AvlCost S 7 2 Inz(12500.00) D AvlPrice S 9 2 Inz(250000.00) D EditAmount S 50 D EditWd C , , **Dollars& &Cents ** CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval EditAmount = Inventory cost is C + %editc(AvlCost : K : *cursym) ** EditAmount = Inventory cost is $12,500.00 ** C Eval EditAmount = Inventory price is C + %editw(AvlPrice : EditWd) ** EditAmount = Inventory price is **250,000*Dollars 00 Cents
OL864445

Copyright IBM Corp. 1996, 2000

4-32

Convert String - XLATE


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Phone S 8 Inz(974 8088 ) D PhoneNbr S Like(Phone) ** D Upper C IVXL D Lower C ivxl ** D Atable S 6 Inz( RPG iv ) D Btable S Like(Atable) ************************************************************* CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++ C : - Xlate Phone PhoneNbr ** PhoneNbr = 974-8088 C Lower:Upper Xlate Atable Btable ** Btable = RPG IV

Copyright IBM Corp. 1996, 2000 4-33

OL864450

Replace String of Characters %Replace


DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Greetings S 50 Inz( We wish you, &C, + D a Happy Birthday! ) D FullName S 30 Inz( Mr. Smith ) D Position S 2 0 CL0N01Factor1+++++++Opcode(E)+Extended-factor2+++++++++++++++++++++++++ C Eval Position = %scan (&C : Greetings) C C C C ** Greetings = We If Eval Position > 0 Greetings = %replace(%trim(FullName) : Greetings : Position : 2)

EndIf wish you, Mr. Smith, a Happy Birthday!


OL864455

Copyright IBM Corp. 1996, 2000

4-34

Copyright IBM Corp. 1996, 2000

4-35

OL864505

Precedence Rules - Using Parentheses


( ) are highest in precedence; use them to:
Break up complex expressions Ensure accurate results

CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ ** Actual Unit Price = ItemPrice - ItemDscnt ** Calculate total price of items ordered (OrdQty) ** C Eval Total = OrdQty * ItemPrice - ItemDscnt ** Above calculation is not correct. Should be written: ** C Eval Total = OrdQty * (ItemPrice - ItemDscnt)

Copyright IBM Corp. 1996, 2000 4-36

CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ ** Correct but ambiguous: C If *IN03 OR Count > Max AND *IN99 ** Better: C If *IN03 OR (Count > Max AND *IN99)
OL864510

Precision - Expressions

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D MarkUpPct1 S 5P 2 D MarkUpPct2 S Like(MarkUpPct1) D List S 11P 2 Inz(4.0) D Cost S Like(List) D Inz(3.0) CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval MarkUpPct1 = (List - Cost) / (Cost / 100) ** MarkUpPct1 = 33.00
Copyright IBM Corp. 1996, 2000 4-37

Eval ** MarkUpPct2 = 33.30

MarkUpPct2 = (List - Cost) / (Cost / 100.00

Expected value of mark up Percentage is 33.33

OL864515

Specifying Precision Rules


Resulting Decimal Positions:

*RESDECPOS on H-spec applies to ALL Evals R-extender without *RESDECPOS applies to single Eval
Maximum Digits: *MAXDIGITS on H-spec (default) applies to ALL Evals M-extender to apply max. digits rules to single Eval

H ExprOpts(*RESDECPOS) DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments ** D PackedN1 s 13p 2 D PackedN2 s 13p 2 D PackedN3 s 13p 2 ** CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ ** Accuracy is maintained to TWO decimal places C Eval PackedN1 = PackedN1 * PackedN2 * PackedN3 ** Accuracy is maintained to THREE decimal places ** because of rounding C Eval (h) PackedN1 = PackedN1 * PackedN2 * PackedN3
Ol864520

Copyright IBM Corp. 1996, 2000 4-38

Precision Using %FLOAT


Dname+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++Comments D Three_00 S 5P 2 Inz(3) D Two_00 S 5P 2 Inz(2) D One S 1P 0 Inz(1) ** D Ans1 S 5P 2 D Ans2 S 5P 2 D Ans3 S 5P 2 CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval Ans1 = (Two_00 / (Three_00 / One)) ** Ans1 is 0.00 C Eval (R) Ans2 = (Two_00 / (Three_00 / One)) ** Ans2 is 0.66 C Eval Ans3 = %Float(Two_00 / (Three_00 / One)) ** Ans3 is 0.66

OL864525

Copyright IBM Corp. 1996, 2000

4-39

%DEC / %DECH / %DECPOS BIFs


D Three_00 S 5P 2 Inz(3) D Two_00 S 5P 2 Inz(2) D One S 1P 0 Inz(1) ** D Ans1 S Like(Three_00) D Ans2 S Like(Three_00) D Ans3 S Like(Three_00) D Pos1 S 3P 0 D Pos2 S Like(Pos1) ** CL0N01Factor1+++++++Opcode(E)+Extended-factor2++++++++++++++++++++++++++ C Eval Ans1 = (Two_00 / (Three_00 / One)) ++++ * |______________| * (30,27) * |________________________| * (30,0) C Eval Pos1 = %Decpos(Two_00 / (Three_00/One)) ** Ans1 is 0.00 Pos1 is 0 C Eval Ans2 = (Two_00 / %Dec(Three_00/One:5:2)) C Eval Pos2 = %Decpos(Two_00 / C %Dec(Three_00/One:5:2)) ** Ans2 is 0.66 Pos2 is 25 C Eval Ans3 = %DecH(Two_00 / (Three_00/One):5:2) ** Ans3 is 0.67
OL864530

Copyright IBM Corp. 1996, 2000

4-40

Copyright IBM Corp. 1996, 2000

4-41

OL86420L

Unit Summary
Having completed this unit, you should be able to:
code calculation specifications using free form operation codes, and

expressions for: assignment of numeric, character, and logical values arithmetic operations logical operations string handling operations
OL864ZZZ

Code Built In Functions in expressions


Copyright IBM Corp. 1996, 2000 4-42

You might also like