![]() |
MPE/iX Commands Reference Manual
> Appendix B Expression Evaluator FunctionsExpression Evaluator Features |
|||||||||||||||||
|
setvar a 1
setvar str2 'b'
setvar boolvar1 1=0
if (a=1)=('a'=str2) or boolvar1 then
EXPRESSION FALSE
endif
Functions may be nested and mixed. String, numeric, and Boolean operations may
not, however, be mixed. For example:
calc 1+'abc'
ERROR
calc 'a' +len('abc')
ERROR
if bound(a) + 3>2 then
ERROR
setvar bool1 true
if bool1 and (str('abc',2,len('ab'))='bc') then
EXPRESSION IS TRUE
endif
calc 'a'+chr(65)
aA
calc 1=3 or 'a' <> 'b'
TRUE
calc chr(ord('A'))
A
calc 2+len(str(lft('abcdefg',2*2),5-3,ord('A')-63))
4, $4, %4
Variables may be used in expressions either through explicit dereferencing or
implicit dereferencing. To explicitly dereference a variable, precede the
variable name with an exclamation point (!). This is passed through
string substitution the same as any other CI command. Explicit dereferencing is
recursive, meaning that if the contents of the variable references another
variable (introduced with an exclamation point) the value of the included
variable is also retrieved.
To implicitly dereference a variable, simply use its name
in any expression. If a variable with this name has not been defined
an error results. Implicit dereferencing is not recursive. This
means that if the contents of an implicitly dereferenced variable
contains a string which might be a variable name, preceded by an
exclamation point the evaluator does not attempt to dereference
that variable. Instead, the string value is used in the expression.
For example:
setvar a 'x'
showvar a
A = x
calc a+'b'
xb
setvar a ''
showvar a
A =
if a = '' then
EXPRESSION IS TRUE
endif
setvar a 'x'
setvar b a
calc a+b
xx
setvar exp 'a+b*c/d'
setvar a 1
setvar b 2
setvar c 3
setvar d 4
setvar e 5
calc exp
a+b*c/d
calc !exp
2, $2, %2
setvar exp2 exp+'*e'
calc !exp2
6,$6,%6
setvar a hptimef
showvar a
A = 8:26 AM ** the time when var was set **
calc a + 'in the morning!!! '
8:26 AM in the morning!!!'
calc 'a' + 'in the morning!!!'
a in the morning!!!'
calc '!a' + 'in the morning!!!
8:26 AM in the morning!!!'
deletevar a
calc a + 'x'
ERROR
setvar hppath '!!hpgroup,pub,pub.sys'
showvar hppath
HPPATH = !hpgroup,pub,pub.sys
calc hppath - ',pub'
!hpgroup,pub.sys
calc !hppath-',pub'
calc UI,pub,pub.sys-',pub'
^
ERROR
comment ** variable dereferenced before call **
comment ** to evaluator and content does not **
comment ** make valid expression. **
calc '!hppath' - ',pub'
UI,pub.sys
setvar a 6+2
setvar b 7
setvar c b
calc b*c
49, $31, %61
calc hpresult/a
6, $6, %6
setvar a '2'
setvar b 6
calc a+b
ERROR ** variables of different types **
calc len(b)
ERROR ** expected string or string variable**
calc ord(a)
50, $32, %62
setvar a -6
calc 18/(3^2^3/3^6/3+6)/-(a+3)-1
-1, $FFFFFFFF, %37777777777
The rules of precedence determine which operations are performed before others.
Their order, from highest to lowest priority, is:
(1=2) And (2=2 or 3=4) FALSE And (whatever) -> FALSE (1=1) or (2=3 and x=y) TRUE or (whatever) -> TRUE
if 6-5>2 and 'abc'-'a'<=rht('cdbc',2) then
EXPRESSION IS FALSE
endif
if not(1=1 and 'a'<>'b') or 6>7 then
EXPRESSION IS FALSE
endif
calc 6+(7>2)
ERROR ** Invalid Expression: **
** Mixed Numeric and Boolean **
if 1 then
ERROR ** Bad Boolean Expression **
setvar errorflag true
if errorflag then
EXPRESSION IS TRUE
endif
The expression evaluator is sensitive to the position
of expression tokens. If an operator is expected, then an operator
must be obtained in that position or a fatal error occurs. If a number
is expected and a valid numeric string is not found, variable management
is called to determine if this token is actually a variable. If
the token is a variable with a numeric value, the variable value
is used in the expression. If the token is not a variable or the variable is
not an integer variable, the expression is not valid and an error is returned.
If a string is expected and a valid quoted string is not found,
variable management is called to determine if the token is a variable.
If it is not a variable, an error is returned. If it is a variable
containing a string value, its contents is used in the expression.
If the variable contains something other than a string, an error is returned.
Provided below is information on other facts you should be aware of concerning
evaluator functions.
OrdIf the length of string> 1, then the value returned from ORD(string) is the ordinal value of the first character in the string. StringsA "string' of characters must be surrounded with quotation marks (" or ') in order to be treated as a string. For example, a + 'a' is treated as the contents of the string variable a concatenated to the string 'a'. Evaluating a string that contains a string operator returns an error unless the string itself is surrounded by quotation marks (" or '). You may include quotation marks within a string in this fashion: "a'b" is evaluated as a'b, but a'b by itself produces an error. You may also use quote folding, for example, two adjacent quotes of the same type that began the string. They are folded to one and the string is not terminated, for example: setvar a "a quote is here""!"This would put the string a quote is here"! into the string variable A. VariablesVariables that are dereferenced by an ! are dereferenced to complete resolution or to the limits of dereferencing (default is 30 levels). Variables may be used in expressions without the !, of course. This is called implicit dereferencing, and these variables are dereferenced to only one level. For example, if variable A has a value of B, it is implicitly and explicitly dereferenced as B. If this variable has a value of !B, implicit dereferencing yields !B. If you want A to be fully dereferenced, you must use !A (explicit dereferencing) in the expression you want evaluated. Variables and StringsExplicitly dereferenced variables should be placed within quotation marks if you want the variable's value treated as a string. Doing this also eliminates problems that might arise if the variable contains delimiters or operators. Refer to the discussion on "Strings" above. For example:
SETVAR X 3
CALC "AB' + "!X'
AB3
CALC "AB' + X
error
CALC "AB" + "X'
ABX
SETVAR Z "foo'
CALC "AB' + Z
ABfoo
CALC "AB' + "!Z'
ABfoo
CALC "AB' + !Z
error variable foo not found
SETVAR A "X'+"Y'
CALC A +"B'
XYB
CALC "!HPTIMEF'
8:26 AM
CALC HPTIMEF
8:26 AM
CALC !HPTIMEF
error
The error in the last example occurs because the dereferenced value of
HPTIMEF is not a valid expression.
Dereferencing of either kind is performed before any evaluation
is carried out. The following examples illustrate the consequences:
SETVAR B 2
SETVAR A B
error
The first command causes no problem. A variable, B, is created and its
value is set to 2. Because 2 is not surrounded by quotes, it
is taken as an integer.
String Substitution assumes that an exclamation point introduces
a variable name. However, there are occasions when the user wants
String Substitution to ignore an exclamation point. Doubling the
exclamation point will cause String Substitution to reduce the two
exclamation points to one, and ignore them as dereferencing characters.
Dereferencing takes place first, and B yields !B For
additional information on variables and dereferencing, refer to the Using
the 900 Series HP 3000 Fundamental Skills (32650-60039). Because B
is not surrounded by quotes, it is not taken as a string, integer, or a
Boolean. Therefore, SETVAR A B produces an error.
The problem is corrected by changing the second command:
SETVAR B 2 SETVAR A "B' ** second command changed **Now the variable A is given the string representation of !B. Consequently, SHOWVAR B B=2 SHOWVAR A A=!BBut, CALC A + 2produces an error. A has been assigned a string value (the result of B, which is the string "!B'). However, CALC !A + 2works. !A is really !B. That in turn yields a value of 2. The result is 2 + 2, which equals 4. CALC "HPTIMEF" HPTIMEFBut, CALC !HPTIMEF CALC 8:26 AMwhich produces an error. On the other hand, CALC "!HPTIMEF"is the same as CALC " 8:26 AM"which produces 8:26 AM
|