Lesson 2 Using Variables and Expressions [ Using the 900 Series HP 3000: Advanced Skills Module 6: Variables and Expression ] MPE/iX 5.0 Documentation
Using the 900 Series HP 3000: Advanced Skills Module 6: Variables and Expression
Lesson 2 Using Variables and Expressions
Introduction
Lesson 2 presents the following information about how the variables can
actually be used, once they have been defined:
* assigning values to variables both interactively and with programs
* displaying and manipulating variable values by using expressions
Using variables in the STATS command file
Look at how the variables HPJOBLIMIT and HPJOBCOUNT are used in this
command file.
ECHO *** CURRENT JOBS = !HPJOBCOUNT
ECHO *** CURRENT JOB LIMIT = !HPJOBLIMIT
IF HPJOBCOUNT<=HPJOBLIMIT-3 THEN
ECHO *** NUMBER OF JOBS IS STILL REASONABLE --- STREAM JOB NOW ***
ELSE
ECHO *** APPROACHING JOB LIMIT --- STREAM JOB LATER ***
ENDIF
ECHO and variable dereferencing.
ECHO displays whatever information follows it. ECHO is often used to
display text messages on the screen. In the STATS file, one line that is
to be displayed is:
*** CURRENT JOBS = !HPJOBCOUNT
The exclamation point will not be displayed, nor will the word
"HPJOBCOUNT." Instead, the current value of HPJOBCOUNT
will replace the word and appear on the screen. This indicates how many
jobs are currently running.
For example, suppose that there are
25 jobs executing on the system. The ECHO command STATS produces this
message on the screen:
________________________________________________________________________
| |
| *** CURRENT JOBS = 25 |
| |
________________________________________________________________________
NOTE When the ECHO command is issued, the CI searches the line for the
special symbol, "!", substitutes a value for the named variable,
and executes the command.
This is called variable dereferencing.
Variable dereferencing refers to the act of substituting the value of a
variable in place of the variable name before executing the command that
contains that variable name. There are two ways to do this (examples
appear on the following pages): implicit dereferencing and explicit
dereferencing.
Explicit dereferencing.
This involves using an exclamation point(!).
This method is similar to the method used with parameters of user
commands. When the system encounters an exclamation point in front of a
variable name, it substitutes the value for the variable name. This
method is available with any command line.
Implicit dereferencing.
This does not involve an exclamation point. It is done automatically,
without an exclamation point.
Implicit deferencing occurs in the commands SETVAR, IF-THEN-ELSE-ENDIF,
WHILE-ENDWHILE, and CALC.
(You will look at IF-THEN-ELSE-ENDIF and WHILE-ENDWHILE later in this
lesson.) The CALC command is a simple calculator command
that will not be discussed; however, its syntax and description are in
the online help facility and in the MPE/iX Commands Reference Manual
Volumes 1 and 2 (32650-90003 and 32650-90364)
Figure 6-1. Implicit and Explicit Dereferencing
--------------------------------------------------------------------------------------------
| |
| Q6-8 Which method of dereferencing is used in the two ECHO statements of the |
| STATS command file? |
| |
--------------------------------------------------------------------------------------------
Dereferencing examples.
You have already seen examples of value assignment with SETVAR, like the
following, where no exclamation point was necessary:
SETVAR USER,HPUSER
HPUSER requires no exclamation point.
If you wanted to set your prompt so that it would echo your user name, as
stored in HPUSER, you would have to use an exclamation point:
SETVAR HPPROMPT,"Hi There !HPUSER"
HPUSER does require an exclamation point in this case.
An exclamation point is necessary if a variable name must be used within
the quotation marks following SETVAR. Without the exclamation point, the
new prompt looks like this:
________________________________________________________________________
| |
| Hi There HPUSER: |
| |
________________________________________________________________________
--------------------------------------------------------------------------------------------
| |
| Q6-9 In an earlier lesson you defined three variables, USER, GROUP, and ACCT. How |
| would you define them again if they were to be given the same values as |
| those currently stored in the system-defined variables, HPUSER, HPGROUP, and |
| HPACCOUNT? |
| |
| SETVAR USER, |
| |
| |
| SETVAR GROUP, |
| |
| |
| SETVAR ACCT, |
| |
| |
--------------------------------------------------------------------------------------------
NOTE You may explicitly dereference a variable in a statement that
requires only implicit dereferencing.
Now look at another case where no exclamation point is necessary, the
IF-THEN-ELSE statement.
IF-THEN-ELSE-ENDIF statement.
In an IF-THEN-ELSE statement, when the IF condition is true, the THEN
action is performed; otherwise (if it is not true), the ELSE action is
performed.
In the STATS command file, a simple IF-THEN-ELSE statement is used to
compare the job limit with the current number of jobs running in order to
determine what action to take.
This means that if the current number of jobs is getting close to the job
limit, a message is displayed indicating that your job should be streamed
later. Otherwise, a different message is displayed. ENDIF marks the end
of the statement.
Figure 6-2. IF-THEN-ELSE-ENDIF Statement
IF condition is true THEN
take this action
ELSE
take a different action
ENDIF
* Note that IF is followed by an optional THEN.
* ELSE is optional and can be included if you have another choice.
* ENDIF must "close" the IF statement.
* IF, ELSE, and ENDIF keywords are not indented, but actions
associated with THEN and ELSE are indented. This helps improve
readability, but is not required.
* No exclamation point is needed in front of the variable names used
in the IF or ELSE conditions:
IF HPJOBCOUNT <= HPJOBLIMIT-3 THEN
Translation:
If HPJOBCOUNT is less than or equal to 3 less than HPJOBLIMIT,
then...
--------------------------------------------------------------------------------------------
| |
| Q6-10 According to the IF-THEN statement in the STATS file, what message is |
| displayed in each of the following situations: |
| |
| |
| a. The current number of jobs is 10, and the job limit is 12. |
| |
| |
| |
| |
| b. The current number of jobs is 5, and the job limit is 10. |
| |
| |
| |
| |
--------------------------------------------------------------------------------------------
Expressions.
Variables in the IF-THEN-ELSE statement are used in conjunction with
certain math operations. These operations are represented by symbols. A
combination of variables, constants, and operators are referred to as an
expression. Variables that appear in expressions do not require
exclamation points (!) to be deferenced. Dereferencing is done
implicitly:
IF HPJOBCOUNT <= HPJOBLIMIT-3 THEN
...action1...
ELSE
...action2...
The first symbol (<=) represents a comparative (relational) operation:
less-than-or-equal-to.
The second symbol (-) represents an arithmetic operation, subtraction.
The following is a basic list of the operations that you can perform in
MPE/iX variables, and the symbols that represent those operations. For a
more exhaustive list, refer appendix B of the MPE/iX Commands Reference
Manual (32650-60002).
Arithmetic Operations:
+ Addition
- Subtraction
* Multiplication
/ Division
Relational Operations:
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
= Equal to
<> Not equal to
Exercise 6-2:IF-THEN-ELSE statement.
1. Write a command file containing an IF-THEN-ELSE statement that
checks to see how many people are on the system. If the current
number is greater than or equal to three-fourths (3/4) of the
session limit, do the following:
a. Print on the screen the current number of sessions and the
session limit.
b. Print the message, ***TOO MANY PEOPLE ON SYSTEM --- LOG OFF
NOW***
Otherwise, do the following:
c. Print on the screen the current number of sessions and the
session limit.
d. Print the message, ***CURRENT SESSION LIMIT NOT
EXCEEDED***.
NOTE Hints are on this page if you need them. Try not to use the hints
until you absolutely have to.
Hints:
* Use ECHO with explicit dereferencing to display the message.
* Use expressions and implicit dereferencing with the IF condition.
* Use (3*HPSESLIMIT)/4 to represent three-fourths of the session
limit value. Remember, variables can only take on integer values,
and 3/4 is not an integer value.
********** End of Exercise 6-2 **********
Using variables in the LF command file:
Let's look at how the variables RESPONSE, FILENAME, and OPTION are used
in the LF command file:
SETVAR FILENAME,"//"
SETVAR RESPONSE,"N"
SETVAR OPTION,"1"
INPUT RESPONSE;PROMPT="DO YOU WISH TO LIST ANY FILE(S)? (Y OR N): "
WHILE RESPONSE="Y" DO
INPUT FILENAME;PROMPT="ENTER FILE OR FILE.GROUP.ACCT &
(@ ALLOWED, // TO END): "
IF FILENAME <> "//" THEN
INPUT OPTION;PROMPT="ENTER LISTFILE OPTION (1,2,3): "
LISTFILE !FILENAME,!OPTION
ELSE
SETVAR RESPONSE,"N"
ENDIF
ENDWHILE
INPUT command.
Other commands may be used to assign a value to a variable. The INPUT
command lets the user interactively assign or change a variable value.
This command can optionally display a prompt on the screen, and the value
that is entered by the user is assigned to the specified variable.
For example, in order for the LF command file to operate correctly, it
must ask the user whether or not it should list a file. INPUT specifies
the variable (RESPONSE) whose value will be supplied by the user; the
PROMPT keyword specifies the prompt that the user sees:
INPUT RESPONSE;PROMPT="DO YOU WISH TO LIST ANY FILE(S)? (Y OR N):
The user sees this:
________________________________________________________________________
| |
| DO YOU WISH TO LIST ANY FILE(S)? (Y OR N): |
| |
________________________________________________________________________
If the PROMPT option were not specified, the user would only see a blank
line and have to guess what to input. It makes more sense to prompt for
input in an intelligent manner, by printing some type of message.
INPUT treats all user responses as string values (series of alphanumeric
characters). This means that if the user enters a digit, it will be
considered a string value, not a numeric value.
To use such digits from an INPUT statement as integers, simply
dereference the variable explicitly, as shown below:
Example:
Suppose that you wish the user to enter a number so that an addition
operation can be performed. Please create a small command file called
ADD1:
INPUT NUMBER;PROMPT="Enter a whole number > "
SETVAR A,!NUMBER + 2
SHOWVAR A
DELETEVAR NUMBER,A
Execute the ADD1 command file. It performs the addition because NUMBER
is treated as an integer (2) since you explicitly dereferenced it as
!NUMBER.
Now create a new command file called ADD2. In it, deliberately do not
explicitly dereference the number:
INPUT NUMBER;PROMPT="Enter a whole number (no fractional part)> "
SETVAR A,NUMBER + 2
SHOWVAR A
DELETEVAR NUMBER,A
Execute the ADD2 command file. You get this error:
________________________________________________________________
| |
| |
| SETVAR A,NUMBER + 2 |
| ^ |
| ILLEGAL CHARACTER FOUND, EXPECTED A STRING (CIERR 9815) |
| |
________________________________________________________________
ADD2 does not perform the addition, since NUMBER is treated as a string
value, not as a numeric value.
NOTE Good practice dictates that you use DELETEVAR to delete your
variables when you are done using them.
WHILE loop.
The WHILE loop is similar to the IF-THEN-ELSE-ENDIF statement in that it
involves conditions and actions. As the name implies, the actions are
performed over and over again (in a loop) until the conditions are no
longer true.
Figure 6-3. WHILE Loop
WHILE condition is true DO
this action
ENDWHILE
As long as the condition is true, the action continues. As soon as the
condition is no longer true, the action stops. Note the following about
the WHILE-ENDWHILE syntax:
* The WHILE statement must always be matched by an ENDWHILE
statement.
* The action associated with WHILE is indented for improved
readability.
* The keyword DO is optional.
--------------------------------------------------------------------------------------------
| |
| Q6-11 |
| According to the LF command file, you are initially asked a question. |
| Until you give a certain response, the system continues to prompt you |
| for file names. What causes this to occur? |
| |
| |
| |
| |
| |
| |
| Q6-12 |
| According to the LF command file, once you are prompted for a file |
| name, what response can you give to terminate further prompting? |
| |
| |
| |
| |
| |
| |
--------------------------------------------------------------------------------------------
Exercise 6-3:while loop.
1. Write a command file called CF that does the following:
a. Asks if you would like to change your prompt.
b. If you answer no, it shows you the current prompt.
c. If you answer yes, it asks you what new prompt you would
like in place of the current prompt character(s) on your
screen. If you enter Return, it assumes that the current
prompt is okay and displays the current prompt with this
message:
_________________________________________________
| |
| |
| *** Current prompt remains in effect *** |
| |
_________________________________________________
Hint: A Return could be represented as "" since the two
quotation marks with nothing between them indicate that
nothing is entered. Make sure that the variable that you
define to hold the Return value is not an integer.
d. If you enter a new prompt, CF prints the new prompt and
asks if it is acceptable. If it is not acceptable, CF asks
for a new prompt again and continues to do so until the
prompt is acceptable.
e. Deletes all of the variables at the end of the file.
2. Execute CF and enter a new prompt, ABC, then another, DEF, and
then finally, !HPUSER: What prompt is finally displayed?
3. Use SETVAR HPPROMPT to change HPPROMPT to the initial system
prompt (:).
********** End of Exercise 6-3 **********
Lesson summary
1. Explicit dereferencing (with an exclamation point) should be used
to display or assign variable values in any command other than
SETVAR, IF, WHILE, and CALC.
2. Implicit dereferencing (no exclamation point) should be used with
SETVAR, IF-THEN-ELSE-ENDIF, WHILE-ENDWHILE and CALC.
3. DELETEVAR should be used to delete user-defined variables (both
the name and the value).
MPE/iX 5.0 Documentation