Implicit Data Types of Literals

Literals express values outside of pre-defined application objects. Because literals have no definition in the Object Dictionary, the software assumes a data type for each literal. This implicit data type is determined based on the characters contained in the value, and on whether or not the value is enclosed in quotation marks.

Literals that are enclosed in quotation marks, or that are unquoted but contain letters, are assumed to be character strings. Character strings are always implicitly CHAR.

Unquoted literal values that contain only digits, or digits and a decimal point are assumed to be numbers. Number literals are always implicitly VASTINT.

Constants (literals defined as objects, with names, in the Object Dictionary) can be assigned either the CHAR or VASTINT data type. No other type is permitted. The assigned data type overrides the data type implicit in the value of the constant.

Examples of Implicit Data Types

1234Is implicitly VASTINT.
abcdIs implicitly CHAR.
2.6Is implicitly VASTINT.
a1b2Is implicitly CHAR.
‘1234’Is implicitly CHAR.
‘ abcd’Is implicitly CHAR.
‘-2.6’Is implicitly CHAR.
‘a1b1’Is implicitly CHAR.

See Also

About Conditional Expressions

Conversion Between Data Types

Data Types and Storage of Values

Data Types and the Use of Database Indexes

How To Use Data Types

Data Types and the Use of Database Indexes

The software automatically determines if it can use indexes when it looks for records in the database. If the software cannot use an available index, each record in the record-containing objects must be scanned (a potentially slower process).

In some cases, the software cannot use an available index if the data type of the indexed field and the data type of the comparison value are incompatible.

Data Type Compatibility in Indexed Database Searches

Data Type of Indexed Field

Required Data Type of Comparison Value (for index to be used)

CHAR, VARCHAR

CHAR, VARCHAR

ALPHA, VARALPHA

CHAR, VARCHAR, ALPHA, VARALPHA

Any number data type, DATE

Any data type

Example

If you enter the command

find Employees where EmpNum = 156

and EmpNum is an Indexed field of a character type, you are asking to compare the number 156 to a character field.

In this case, the software does not use the available index, because to do so would produce an incorrect result. The index is sorted internally, based on character comparisons. The literal 156 is implicitly a VASTINT number and cannot be effectively compared to character values.

Instead, the software performs the FIND by checking the 156 against the individual values of EmpNum. Speed of execution is slower than if the index could be used.

To ensure that the index is used, modify the command to read

find Employees where EmpNum = ‘156’

In this case, the literal, because it is enclosed in quotation marks, is implicitly a character data type (CHAR), and can be compared to the indexed EmpNum values, which are explicitly character type.

Data Types and Storage of Values

ALPHA/CHAR and VARALPHA/ VARCHAR Values

If an ALPHA or CHAR object is assigned the value abc///// (with five trailing blanks), it is stored as abc///// (fixed length value).

If a VARALPHA or VARCHAR object is assigned the value abc/////, it is stored as abc without any trailing blanks, but with an indicator that gives the length of the value (variable length value).

Maximum Lenth for ALPHA/CHAR and VARALPHA/VARCHAR

 

ALPHACHARVARAPLHAVARCHAR
32767 bytes32767 bytes32767 bytes32767 bytes

INT and LONGINT Values

INT, LONGINT and VASTINT values are stored “without decimals”. Actually, the decimal information is stored separately.

The “non-decimal” value (value of the significant digits) must fall within the range of the applicable data type.

Examples – INT Values

ValueSignificant DigitsNumber of DecimalsValid Maximum/Minimum Values
123.4123413276.7 / -3276.8
12.3412342327.67 / -327.68
12.341234332.767 / -32.768
0.01234123450.32767 / -0.32768

Examples – LONGINT Values

ValueSignificant DigitsNumber of DecimalsValid Maximum/Minimum Values
1.23456123456521474.83647 / -21474.83647
1234.56123456221474836.47 / -21474836.47

See Also

About Conditional Expressions

Conversion Between Data Types

Data Types and the Use of Database Indexes

How To Use Data Types

Conversion Between Data Types

Zim performs conversions and operations between data types in a different manner than other application development languages. The rules for conversions are embedded in Zim itself, and when performing conversions or comparisons, these rules govern how the different data types are handled.

Implicit Conversions

In general, when you write an expression, you can use objects or literals of any data type, in any combination. However, when the software encounters an expression containing values with mixed data types or a data type incompatible with the requirements of the expression, it applies a number of rules for converting the data types should conversion be necessary.

Character-to-character Comparisons

  1. When values are being compared, VARALPHA and VARCHAR values are treated as if they were ALPHA and CHAR values, respectively.
  2. When two values of type ALPHA are being compared, an ALPHA-type (case insensitive) comparison is performed.
  3. When two values of type CHAR are being compared, a CHAR-type (case sensitive) comparison is performed.
  4. When two values are being compared, one of type CHAR and one of type ALPHA, an ALPHA-type (case insensitive) comparison is performed.

Character-to-number Conversions

  1. When two values – only one of which is a number type – are compared or are processed in an arithmetic operation, the value that is not a number type is converted to a number and a numeric comparison or operation is performed.

Number-to-character Conversions

  1. If a number (type INT, LONGINT, VASTINT) must be converted to a character string for use by a command or function (e.g., to be output by the LIST command or processed by $ substring), the number is converted and right-justified, always producing a result string long enough to hold the largest possible value for that data type (8 characters for INT; 12 characters for LONGINT; 17 characters for VASTINT).
  2. Number literals implicitly take the data type VASTINT and assume a number of decimal places equal to the number that actually appear in the literal. If the software has to convert a number literal to a character string for use by a command or function, it becomes a character string 17 characters long, as indicated in Rule 6.

Date Comparisons

  1. When two values are being compared, only one of which is type DATE, the other value is converted to DATE (if possible) and a DATE comparison is performed.
  2. If a number is converted to a date, it is assumed to be in a YYYYMMDD form (e.g., 19991122 is November 22, 1999).

Arithmetic Operations Involving Dates

  1. When an arithmetic operation is being performed on two values, at least one of which is type DATE, the operation is carried out as summarized below:
val1 + val2

If only one value is type DATE, the other is converted to a number and added (as a number of days) to the date.
If both values are type DATE, a normal arithmetic operation is performed and the result is treated as a number.

val1 – val2

If both values are type DATE, the dates are subtracted to yield the number of days between the two dates.
If val1 is type DATE and val2 is not, val2 is converted to a number (if necessary) and is subtracted (as a number of days) from the date.
If val2 is of type DATE and val1 is not, both values are converted to numbers (if necessary) and a normal arithmetic operation is performed.

val1*val2
val1/val2
val1^val2

Both values are converted to numbers (if necessary) and a normal arithmetic operation is performed.

Date-to-character Conversions

  1. If a DATE value must be converted to a character string, it becomes a string eight characters in length.

Explicit conversion Using Software Functions

Although values are implicitly converted from one data type to another as required, the software provides a number of functions that forcibly convert values from one data type to another.

The conversion functions are $ toalpha, $ tocharacter, $ todate and $ tonumber.

 

See Also

=, , >, >= (Condition)

About Conditional Expressions

Data Types and Storage of Values

Data Types and the Use of Database Indexes

How To Use Data Types

Program Templates and the GENERATE Command

Although every application has its own unique requirements, the individual components of applications are often very similar.

For example, menus, pop-up windows, and data entry forms and displays are common components of most applications. Pop-up windows, for instance, can vary in size, location and contents, but the commands to present them to the application user and to process the result of user interaction are essentially the same.

While Zim provides a great deal of flexibility in how applications can be designed and developed, experienced Zim programmers typically develop a programming style for various components of their applications. Consistency of style means that the application program code for one application object can be copied and modified when another, similar object is needed.

If a new pop-up window is needed, for example, the object definition and processing code for a similar, existing pop-up can be copied, and the necessary minor changes made. This approach can reduce development time considerably.

Template Definition

The template feature for Zim is a formal mechanism for producing reusable program code.

A template is an application program that contains the basic code for defining, presenting, and processing an application component such as a standard pop-up window. In addition, the template contains fragments of code that

  • customize the basic code

  • generate the application objects (e.g., windows, forms, variables) needed by the application components

Template Syntax

A template is similar to any other application program, in that it contains a series of command statements. However, a template is also different, in that it contains sections of code bounded by special template delimiters: << and >>.

The template delimiters are always placed at the start of a line; nothing else should appear on the same line. In this example,

… commands 1 …
<<
… commands 2 …
>>
… commands 3 …

the software handles the command statements that appear between the template delimiters differently depending on if the program is being executed, parsed, or compiled, or if it is being run under the GENERATE command.

During normal execution, commands 1 and commands 3 execute normally. Commands 2 are ignored.

With the PARSE or COMPILE command, commands 1 and commands 3 are parsed or compiles in the usual manner. Commands 2 are ignored.

With the GENERATE command, commands 1 and commands 3 execute normally. Commands 2 are sent as output, after macro substitution, to the current output device.

Program Call Syntax

The syntax of the call to the template program in the GENERATE command is identical to the structure of a call to a standard application program of the same type.

The rules for passing values to the parameters in a template program containing a formal procedure operate in exactly the same way as for an application program containing a formal procedure.

Program Execution

Program execution is handled slightly differently under GENERATE. Execution always proceeds interpretively, even if the template program has been compiled.

When the software encounters the << template delimiter alone on a line, execution is suspended, and the subsequent lines are written – after macro substitutions – to the current output destination. Execution resumes when the software encounters the >> template delimiter alone on a line.

Physical Line Handling

Normally, each physical line in the template is sent as a separate line to the current output destination.

To cause successive physical lines in the template to be joined into one line in the output, end the appropriate lines in the template with the dollar sign ($). At the output destination, the physical line containing the dollar sign and the next physical line appear concatenated on one physical line.

If a line must end with a dollar sign for another reason, direct the software to interpret the dollar sign literally by placing a backslash ( – Escape) preceding the dollar sign.

Arithmetic with Dates

ZIM-X provides a number of built-in functions to perform date and time arithmetic.  If an arithmetic expression subtracts one date from another (i.e., both operands are of data type DATE), the result is a number representing the number of days between the two dates.

Example

Consider the Date Type values: Date1 = 19990923 (23SEP1999) and Date2 = 19980923 (23SEP1998).  We can now apply arithmetic to the dates by simply doing the following:

Date1 – Date2 evaluates to 365. Because the numbers are Date Types, ZIM equates to Date1 to 19990923 and Date2 to 19980923. If the same calculation was performed with INT Data Types the result would be 10,000.

If you add (subtract) a value that is not a date to (from) a date, the result is a date equal to the date operand plus (minus) the number of DAYS indicated by the non-date operand.

Date1 - 100 equates to 19990923 - 100, but evaluates to 19990615 (15Jun1999 - not 19990823)
Date1 + 100 equates to 19990923 + 100, but evaluates to 20000101 (1Jan2000- not 19991023)

Comments

You can covert a Number Type value to do a Date Type value by using $todate().

If you use dates in any other type of operation, the dates are treated as numbers:

Date1/100 equates to 19990923/100, and evaluates to 199909.

See Also

$addyears

$addmonths

$addminutes

$addhours

How To Construct Arithmetic Expressions

Writing Expressions

Arithmetic operators and parentheses can be combined to create long, complex expressions. Arithmetic expressions are evaluated based on the standard rules of precedence. Parentheses alter the order of evaluation.

Spaces between operands and operators can be used for clarity, but are not required.

For example, Salary * 1.1 and Salary*1.1 are equivalent.

Data Types

Arithmetic expressions yield a numeric result (if possible).

Operands should preferably be of a numeric data type, but character types are converted into an equivalent number as shown in the following example:

” ” (a space) is 0.

“-2″+5 is 3.

“2”*”-4″ is -8.

If a character value cannot be converted to a number (e.g., “abcd”), an error message is returned and the value is treated as 0.

Decimals and Rounding

The result of an arithmetic expression has as many decimal places as are found in the operand with the most decimal places in the expression. If the result is assigned to a field, variable, or form field that has fewer decimal places than the result, the result takes on the number of decimal places in the field, variable, or form field. Results are rounded as required. Rounding occurs only after the entire expression has been evaluated as shown in the following examples:

5/2 is 3

5/2.0 is 2.5

1+1.01 is 2.01

1.01+(5/2) is 3.51

How To Use Arithmetic Expressions

An arithmetic expression is a complex value expression that consists of two or more value expressions and associated arithmetic operators.

The arithmetic operators are

  • + (Add)
  • – (Subtract)
  • ^(Exponentiation)
  • * (Multiply)
  • / (Divide)

Examples of Arithmetic Expressions

For clarity, spaces can be used between operands and operators, but they are not required.

Salary * 1.1

or

Salary*1.1

Salary + (Salary * 0.1)

(1 + InterestRate)^Years

TotalPrice/Items

SellingPrice – (SellingPrice * BulkDiscount)

See Also

About Arithmetic Expressions

Arithmetic with Dates

Decimals and Rounding

Rules of Precedence for Operators

Rules of Precedence for Arithmetic Operators

Operator

Rule of Precedence

Unary + and

Positive (+) and negative (-) signs are evaluated first, e.g., +2 or -Salary.

^

Exponentiation (^) is performed next, e.g., x^y, that is x raised to the power of y.
Note: If x is negative, y must be an integer.

* and /

Multiplication (*) and division (/) are performed following exponentiation.

+ and

Addition (+) and subtraction (-) are performed last.

Notes:

  • Operators of equal precedence are evaluated from left to right in the expression.

  • Parentheses can be used to explicitly determine the order of evaluation, overriding the order described above. Each pair of parentheses can contain only one arithmetic expression. Use parentheses extensively in arithmetic expressions.

en_CAEnglish