Previous | Contents | Index |
Variables specify storage locations. Numeric and string variables are assigned values with the LET statement. String variables can also be assigned values with the LSET, RSET and CSET statements.
The LET statement assigns the value of an expression to a variable. The expression is evaluated and its value is stored in the location specified by the variable. The data types of the expression and the variable must match. Thus, you must assign a string variable string values and you must assign numeric variables numeric values. A string variable must end with a dollar sign "$" unless you declare it. An integer variable must end with a percent sign (%) unless you declare it.
The DECLARE statement allows you to dispense with data type designations. DECLARE declares a variable as either string, integer or real. Once the variable has been declared, you do not need to attach a $ or % to it. Functions, arrays, etc., can also be declared with the DECLARE statement.
DECLARE [STRING | INTEGER | REAL | BOOLEAN | OBJECT] var, var... |
Example 5-1 DECLARE statement |
---|
declare string name, sex declare integer age declare real amount declare object anything declare boolean is_male input 'Enter your name': name input 'Enter your age': age input 'Enter your sex': sex input 'Enter an amount': amount if sex = 'male' then is_male = true else is_male = false print print name; ' is a'; age; 'year old '; sex print name; ' entered the amount'; amount if is_male then print name; ' is male' anything = 4 + 5 print 'This object (or dynamic) variable contains a numeric value: '; anything anything = 'kitty' print 'Now it contains a string value: '; anything end Enter your name? Sammy Enter your age? 28 Enter your sex? male Enter an amount? 25.38 Sammy is a 28 year old male Sammy entered the amount 25.38 Sammy is male This object (or dynamic) variable contains: 9 Now it contains a string value: kitty |
DECLARE is used to specify the data types of variables, functions, etc. Once the data type of a variable or function has been declared, it is not necessary to designate them with a trailing $ or %.
DECLARE declares the data type of a variable or function. The STRING option indicates that the following are string variables or functions. INTEGER declares integer numeric. REAL indicates real numeric. The BOOLEAN option indicates that the following are Boolean variables. Only one of the four data type options can be used in each DECLARE statement. Any number of variables can be declared with a DECLARE statement.
DECLARE OBJECT declares one or more variables to be of type OBJECT. A variable of type OBJECT receives the data type of the data that is put into it.
OBJECT and DYNAMIC are synonyms in this statement. DECLARE DYNAMIC is the same as DECLARE OBJECT. |
To find out the current data type of an object variable, the DTYPE function can be used (see Section 6.9.2).
Multiple data types can be declared by using the following format:
DECLARE datatype var, var, datatype var, var, datatype var, var, ... |
For example:
Example 5-2 Declaring Multiple Data Types |
---|
declare string name, sex, integer age, year, real amount input 'Enter your name': name input 'Enter your age': age input 'Enter your sex': sex input 'Enter the year': year input 'Enter an amount': amount print print name; ' is a'; age; 'year old '; sex; ' in'; year print name; ' entered the amount'; amount end Enter your name? Terry Enter your age? 25 Enter your sex? female Enter the year? 2000 Enter an amount? 582.69 Terry is a 25 year old female in 2000 Terry entered the amount 582.69 |
DECLARE TABLE table_name1 [, table_name2 ...] |
Example 5-3 DECLARE TABLE |
---|
declare table str open table cl: name 'sptools:client' ask table cl: id cl_id$ set table str: id cl_id$ extract table str end extract for each str print str(#1); ' '; str(#2) next str end 20000 Smith 20001 Jones 20002 Kent 23422 Johnson 32001 Waters 43223 Errant 80542 Brock 80543 Cass 80544 Porter 80561 Derringer 80573 Farmer |
DECLARE TABLE declares one or more symbols to be of type TABLE. Once a symbol has been declared to be of type TABLE, it can be used in statements such as SET TABLE...ID to write generalized routines where you do not know at compile time which table you are going to use.
Usage example: this statement could be used in the situation where you have a transaction table and a transaction history table and, optionally, want a report on one or the other. You could use one report program and the DECLARE STRUCTURE statement to declare which table to use when the user makes the report selection.
5.2 OPTION Statements
5.2.1 OPTION REQUIRE DECLARE
OPTION REQUIRE DECLARE |
Example 5-4 OPTION REQUIRE DECLARE Statement |
---|
option require declare declare string name, comment input 'Please enter your name': name line input 'Enter a comment in quotes': comment print name; ' says, '; comment end Please enter your name? George Enter a comment in quotes? 'Have a nice day!' George says, 'Have a nice day!' |
OPTION REQUIRE DECLARE causes Sheerpower to require all variables in the program to be declared. If the OPTION REQUIRE DECLARE statement is used and a variable is left undeclared, Sheerpower will return an error when program execution is attempted. The OPTION REQUIRE DECLARE statement should occur before any DECLARE statements and before any variables are assigned.
OPTION BASE [0 | 1] |
OPTION BASE sets the lowest subscript or base for arrays. The base can be either zero or one. If you use OPTION BASE 0, the lowest element of an array has the subscript zero (0). If you use OPTION BASE 1, the lowest element is subscript one (1).
See Section 5.8.4 for an example and detailed information on this statement.
[LET] var = expr |
Example 5-5 LET Statement |
---|
input 'Last name': last$ input 'First name': first$ let name$ = first$ & ' ' & last$ print name$ end Last name? Taylor First name? Rick Rick Taylor |
The LET statement is used to store information into a variable or data table.
var is the variable being assigned a value. expr is an expression. The expression is evaluated and its result is assigned to the variable. The expression can be any Sheerpower expression (see Chapter 4.) The variable and the expression data types must match. For instance, if var is a string variable, expr must be a string expression.
NOTE: The keyword LET is optional. For example:
LET name$ = first$ & ' ' & last$ |
can be stated as:
name$ = first$ & ' ' & last$ |
When Sheerpower executes the LET statement, it first evaluates the expression on the right side of the equal sign. It then assigns this value to the variable on the left side of the equal sign. The variable represents a location in memory. The value of the expression is stored in this location. Each time a new value is assigned, the old value is lost and the new value is stored in its memory location.
Assigning Numeric Values:
Example 5-6 Assigning Numeric Values with LET Statement |
---|
input 'Amount': amount let rounded% = amount print 'Real numeric amount:'; amount print 'Integer amount (after rounding):'; rounded% end Amount? 1.54 Real numeric amount: 1.54 Integer amount (after rounding): 2 |
Previous | Next | Contents | Index |