|
B.1 Variables, Data Types, Database access, and Declaring
|
First and foremost, have fun when programming with Sheerpower. It is fast.
It is intuitive.
Variable names
User variable names must start with a letter and can include letters,
numbers, the underscore ("_"), and an embedded dollar sign ("$"). Names are
case-regardless. This means that
Account is the same as
AccOUNT. If a variable name consists of multiple words, by convention
each word is separated by an underscore.
Sheerpower data types
- Real -- A number having up to 18 digits to the left of the decimal and 16 to the right.
To support business applications, Sheerpower uses
Perfect Precision Math
which eliminates
rounding errors
when performing simple math operations.
- String -- Characters, such as a person's name, city name, and also arbitrary binary 8-bit data.
- Integer -- A whole number without fractional digits.
- Boolean -- A logical of true (1) or false (0). Used in conditional statements.
- Dynamic -- Can be any data type. Adjusts its data type according to use.
- Database access -- expressed by table_name(field_name)
Of these, the most commonly used data types are REAL, STRING, and BOOLEAN --
in that order. For business applications, INTEGER and DYNAMIC data types are
less frequently used and rarely even needed.
All database access goes through the
table_name(field_name) syntax. For example
print payroll(salary) will fetch the current
payroll table record, lock it if needed, extract the
salary field, and print out its contents.
The statement
payroll(salary) = 1234.56 will fetch the current record, lock it, and store the
value
1234.56 into the payroll salary field. The modified record will be written and
unlocked when needed using a "lazy write" method that guarantees the integrity of the table.
This process makes Sheerpower immune to
SQL injection attacks. For details, go
here.
Variables with a dollar sign "$" suffix default to being a
string. Those with
a percent sign "%" suffix default to being an
integer. And those with a
question mark "?" suffix default to being a
boolean. Unsuffixed, undeclared
variables are assumed to be declared as
real.
print 1.2 - 1.0
tax_rate = 5.5
fullname$ = 'Sally Sue'
is_wonderful? = true
counter% = 50
declare dynamic answer
declare real inches
declare string address
declare integer frogs
declare boolean is_great
inches = 55.6
print inches
You can also require that all variables be declared. In this case, undeclared
variables will cause compile-time errors.
option require declare
Declaring Output Formatting
There are some variables that when printed you always want
specific formatting to be used. For example, with a variable that contains cash, every time it is
printed, you might want to include a dollar sign, commas, and also rounded to two decimal digits.
For details on print formats, see the
SPRINTF() function
declare format '$%.2m' cash
cash = 123456.7
print 'The value is: ';cash
The value is: $123,456.70