Variables have many uses at the R> Prompt as well as all other modules of R:BASE. You can define variables or use expressions to calculate subtotals and totals, manipulate text values, draw data from tables, perform complex mathematical operations, return financial data such as interest rates, or perform many other functions.
A variable is an item that holds data. It is similar to a column, except that a variable is not connected to any particular table in a database. You can define a variable at the R> Prompt and then use the contents of the variable with more than one database. Variables, then, are global within R:BASE.
An expression is the computation that gives a variable its value. Expressions can also provide the value of a column as in computed columns, or they can be used to calculate values on the fly with the SELECT command.
Follow the below instructions to work with variables at the R> Prompt
1. | Enter SET VARIABLE v1 = 100 |
This defines a variable named v1 to a value of 100. Since you did not specify a data type, R:BASE automatically assigns an INTEGER data type because 100 is a whole number without a decimal point.
2. | Enter SHOW VARIABLE |
R:BASE displays the list of variables, both the system variables (#DATE, #TIME, #PI, SQLCODE, SQLSTATE, and #NOW) and the variables you define. The display looks similar to this:
R>SHOW VARIABLE
Variable = Value Type
------------------ ------------------------------ -------
#DATE = 06/05/2014 DATE
#TIME = 1:55 PM TIME
#PI = 3.14159265358979 DOUBLE
SQLCODE = 0 INTEGER
SQLSTATE = 00000 TEXT
#NOW = 06/05/2014 1:55 PM DATETIME
v1 = 100 INTEGER
You can assign the data type of a variable before or after any value is placed in it as long as the value and the data type are compatible.
3. | Enter SET VARIABLE v1 REAL |
v1 already has the INTEGER value 100, but the data type can be changed to REAL.
4. | Enter SHOW VARIABLE v1 |
On the screen, R:BASE displays "100." When you include the variable name on the line with SHOW VARIABLE, only the specified variable value is displayed. R:BASE does not display the heading or data types that are shown with the SHOW VARIABLE command when it is used without a variable name.
You can abbreviate most commands in R:BASE to the first three characters. If the command has more than one keyword, then each keyword can be abbreviated. Some secondary keywords can be abbreviated to a single character. For example, SHOW VARIABLE can be abbreviated to SHO VAR or SHO V.
You can combine text values together in a variable.
5. | Enter SET VAR text1 = 'Now is the time' |
6. | Enter SET VAR text2 = 'for all good men' |
7. | Enter SHOW VAR |
As you can see, text1 and text2 are assigned TEXT data types.
9. | Enter SET VAR text3 = (.text1 & .text2) |
This combines the two text variables text1 and text2 together into one variable, text3. When you want to use the value of a variable, you precede the variable name with a dot, or period. This tells R:BASE to use the value of the variable, not just the name, as a text string. The ampersand "&" combines text strings and places a space between them. A plus "+" combines text strings without a space.
10. | Enter SHOW VAR text3 |
R:BASE displays "Now is the time for all good men" on the screen. Because the default display width of a TEXT variable is 30 characters, the last word, men, wraps to a second line.
After entering each of the commands, note that the Command History panel stores all of the commands entered. To reenter any command listed in the Command History panel, double click on it. To display the command at the Input Console, select it.
You can specify the width to display variables.
11. | Enter SHOW VAR text3 = 35 |
Notice that the last word does not wrap because you allowed enough space for the full line by adding = 35 following the variable name.
You can do arithmetic for numeric variables as well.
12. | Enter SET VAR v1 = (.v1 + 25) |
13. | Enter SHOW VAR v1 |
R:BASE added 25 to the value of the variable v1 to get 125.
R:BASE has a large number Functions included. Functions perform a series of set calculations or convert values into other forms.
14. | Enter SET VAR v2 = (SQRT(.v1)) |
Be sure that you always enclose in parentheses any expression containing a function.
15. | Enter SHOW VAR v2 |
The SQRT function has calculated the square root of the value of v1 and the SET VARIABLE command has placed that value into variable v2. The result is a DOUBLE (double-precision real) number.
You can combine many items and functions in a single expression.
16. | Enter SET VAR v3 = (LOG10(.v1) * SQRT(.v1) / EXP(.v2) * 15) |
Use parentheses around expressions to make sure that R:BASE recognizes an expression as an expression rather than a text string. R:BASE calculates expressions more quickly if it does not have to first decide whether or not an expression is to be calculated or simply be treated as a text string.
17. | If you want, look at variable v3. |
R:BASE displays the DOUBLE data type number 0.004904177044216
You might wonder how you might use these variables once they are defined. Variables have many uses including:
•Calculating the value for a column from other column values, as with computed columns
•Providing a value for comparison to other values to determine if another operation should be performed, as in programs
•Collecting data from a table to use in a calculation
•Passing data between tables or databases
•Temporarily storing totals for the current session
You do not need to keep the created variables, so remove them now. Global variables may be deleted individually or all together. To delete variables, you use the CLEAR command. CLEAR completely removes the variables, not just the value of the variable.
18. | Enter CLEAR ALL VARIABLES (or the abbreviated form CLE ALL VAR). |
If you enter SHOW VARIABLE again, you will see that R:BASE has removed all the global variables you entered. Only the system variables remain.
If you want, you can exit from R:BASE at this time.
19. | At the R> Prompt, enter EXIT |
You are then returned to the operating system.