basic.intro Introductory/BASIC Program

basic.intro

Command basic.intro Introductory/BASIC Program
Applicable release versions: AP 6.2
Category BASIC Program (486)
Description a high-level programming language, is the principal programming language bundled with the Pick System and was designed for the implementation of application requirements not handled by standard Pick functionality.

Pick/BASIC source can contain any number of statements in any order and can be stored as programs or subroutines called by other programs or processing codes. Pick/BASIC programs are created in the user's editor of choice and reside as items within a file. Prior to execution, the programs must be compiled to convert the program into object code. Compiled programs are stored in the dictionary of the file that contains the source code. Compiled programs or subroutines can be cataloged as verbs withing the users master dictionary (Refer to the TCL commands compile, catalog, decatalog, and run).

Program Statements

A Pick/BASIC program is composed of statements made up of Pick/BASIC commands, variables, constants, expressions, and functions. Pick/BASIC statements may contain arithmetic, relational, and logical expressions. These expressions are formed by combining specific operators with variables, constants, or Pick/BASIC intrinsic functions. The value of a variable may change dynamically throughout the execution of a program. A constant, as its name implies, has the same value throughout the execution of a program. An intrinsic function performs a pre-defined operation on a supplied parameter(s).

Normally, within a Pick/BASIC program, the programmer writes only one statement per line. This is done for the ease of reading and tracking of logic. More than one statement can be placed on a program line by separating them with semicolons. Statements which end with "then" or "else" on a line create a block or multiline structure. See the "then/else" entry for details on the "then ...else" statements.

Labels may be placed at the beginning of any Pick/BASIC statement. A statement label may be numeric or alphanumeric. If it is alphanumeric, it must begin with a letter and may be any combination of letters, numbers, periods, or dollar signs and can end with an optional colon. The colon is also optional with a numeric label. Refer to the entry "statement labels".
Except for situations explicitly called out in the following sections, blank spaces appearing in the program line which are not part of a literal are ignored. Blank spaces and lines may be used within the program for purposes of appearance.

Data Representation

There are two types of data used in Pick/BASIC: numeric and string. Numeric data consists of a series of digits and represents an amount, such as 255. String data consists of a set of characters, such as Jan Smith. Refer to the entry data representation for more information.

Variables

Variables are values that can change. Values can be assigned with an assignment statement. Assignment statements assign the value of an expression to a variable. A variable can be a simple variable, an array element, a dynamic array element, or a substring. For example:

var=x+2

assigns the value of the expression x+2 to the variable var. Refer to the entry assignment for more information about assignment statements.

Arrays

An array is a table of values of any data type. Each value in an array is called an element. The elements in an array are ordered allowing each array element to be accessed by specifying its position within the array.

Pick/BASIC supports two types of arrays: dynamic arrays and dimensioned arrays. Dimensioned arrays can be defined with the dim statement, which specifies the name of the array and the number of elements in the array. Or, dimensioned arrays can be defined with the file statement.

Dimensioned arrays are limited to two dimensions (rows and columns). Additional dimensions can be obtained by referencing dynamic arrays from elements in a dimensioned array.

A dynamic array is a string containing attribute marks, value marks, and/or subvalue marks which are used as field delimiters. All elements within dynamic arrays are separated by one of these delimiters. Dynamic arrays do not have a fixed size nor are they dimensioned. Subscripts in dynamic arrays are enclosed in angle brackets (<>). They are called dynamic because the number of elements can be increased or decreased automatically by the system without the programmer needing to recompile the program.

In most cases, accessing a dimensioned array is faster than accessing the same size dynamic array. A dynamic array may be an element in a dimensioned array, in which case the dynamic array subscripts are specified following the dimensioned array subscript. Refer to the entries array, dimensioned array, and dynamic array for more information about arrays.

Operators

Expressions are formed by combining operators with variables, constants, or functions. When an expression is encountered as part of a Pick/BASIC program statement, it is evaluated by performing the operations specified by each of the operators on the adjacent operands.

Each operator has a precedence rating and, in any given expression, the highest precedence operation is performed first. (0 is the highest precedence and 8 is the lowest. If there are two or more operators with the same precedence or if an operator appears more than once, the leftmost operation is performed first. Precedence of the operators is as follows:

Operator Operation Precedence
( ) Express evaluation 0 -- highest
[ ] Substring extraction 0
^ ** Exponent 1
- Unary minus 2
* Multiplication 3
/ Division 3
+ Addition 4
- s Subtraction 4
expr expr Format string 5
cat (:) Concatenate 6
= < > # Relational operators 7
<> ><
<= >=
match Pattern matching 7
and & Logical operators 8 -- lowest or !

Parentheses may be used anywhere to clarify evaluation, even if they do not change the order. The parenthesized expression as a whole has highest precedence and is evaluated first. Within the parentheses, the rules of precedence apply.

The function associated with the *=, +=, -=, /= assignment statements take place after all left side operations and before the assignment.

Refer to the individual entries for each operator and the entries logical operators, operators, pattern matching, precedence, relational operators, and special characters.

Arithmetic Operators

Arithmetic expressions are formed by using the arithmetic operators. The simplest arithmetic expression is a single numeric constant, variable, or function. A simple arithmetic expression may combine two operands using an arithmetic operator. More complicated arithmetic expressions are formed by combining simple expressions using arithmetic operators.

If a string value containing only numeric characters is used in an arithmetic expression, it is evaluated as a decimal number. If a string value containing non-numeric characters is used in an arithmetic expression, a warning message is printed and zero is assumed for the string value.

Format Strings

Numeric and non-numeric strings may be formatted using format strings, which consist of numeric masks and format masks. The numeric mask code controls justification, precision, scaling, and credit indication. The format mask code controls field length and fill characters.

The entire format string is enclosed in quotation marks. If a format mask is used, it should be enclosed in parentheses within the quotation marks. The entire format string may be used as a literal, or it may be assigned to a variable. A format string literal can immediately follow the string it is to format. A format string variable must be separated by at least one space from the string it is to format. The format string may also be used directly in conjunction with the print statement. For more information, refer to the entry masking.

String Expressions

A string expression can be a string constant, a variable with a string value, a substring, or a concatenation of string expressions. String expressions may be combined with arithmetic expressions.

Concatenation of strings is specified by a colon (:) or by the operator cat. The operator cat must be preceded and followed by blanks. Refer to the entry string expressions.

Substrings

A substring is a set of characters that makes up part of a whole string. The syntax to specify a substring is:

substring = string[m,n]

where "string" is a string variable, "m" is the starting character position, and "n" is the substring length. For example, if the current value of variable S is the string "ABCDEFG", then the current value of S[3,2] is the substring "CD" (the two-character substring starting at character position 3 of string S).

If the starting character specification is past the end of the string value, then an empty substring value is selected. If the starting character specification is negative or zero, then the substring is assumed to start at character one.

If the substring length specification exceeds the remaining number of characters in the string, then the remaining string is selected. If the substring length specification is negative or zero, then an empty substring is selected.

A segment of a character string may be changed or substituted without affecting the remainder of the string by assigning the new segment to the variable containing the original string. The new segment is assigned to the original string variable with the starting character and segment length specified with the variable. The string variable may be a single variable or an array element. For example, if the current value of the variable S above ("ABCDEFG") is to have characters 3 to 5 inclusive replaced by the string "123" the assignment S[3,3]="123" accomplishes this substitution so that the variable S now becomes "AB123FG". The general syntax for substring replacement is:

string[m,n] = substring

where "string" is a string variable, "substring" is the substring to be inserted, "m" is the starting character position, and "n" is the number of characters in the string which are replaced. See the Advanced Pick Reference Manual "substring assignment" token for more details.

One or more fields or substrings within a character string variable which are delimited by a specific character can be changed by a single assignment.

string[delimiter,m,n] = substring (s

where "string" is a string expression or variable, "substrings" is one or more substrings (separated by a delimiter) to be substituted, "delimiter" is the delimiter separating fields or substrings in the original string, "m" is the starting field position within the string for the substring to be placed, and "n" is the number of fields or substrings to be replaced.

If m is less than 1, 1 is assumed. If m is greater than the number of fields within string then the number of fields in string is increased by adding null fields separated by the delimiter so that string has the length of m fields. Multiple substrings separated by the specified delimiter can be substituted in the original string.

The expression n has different effects depending on whether it is positive, negative or zero. If it is positive, then n fields in string are replaced with n substrings. If m + n is greater than the number of fields in string, then the number of fields in the resulting string is increased to the value m + n, and the extra substrings are concatenated to the end of the original string. If m + n is less than the number of fields in string but n is greater than the number of substrings specified, only the specified substrings are substituted and the remainder of the n fields in string are nulled.

If n is zero, then no fields in string are deleted and the specified substring(s) are inserted into string before the mth field. If n is less than zero, then n fields greater than or equal to m are deleted from string and the entire expression containing the substring(s) is inserted at that point in the string.

Relational Expressions

A relational expression evaluates to 1 if the relation is true, and evaluates to 0 if the relation is false. Relational operators have lower precedence than all arithmetic and string operators. Therefore, relational operators are only evaluated after all arithmetic and string operations have been evaluated.

If one operand is numeric and one operand is string, both operands are treated as strings. To resolve a numeric relation, the values of the expression are compared. To resolve a string relation, the characters in each string are compared one at a time from leftmost characters to rightmost. If no unequal character pairs are found, the strings are considered to be equal.

If the two strings are not the same length, and the shorter string is otherwise identical to the beginning of the longer string, the longer string is considered greater than the shorter string.

The resolution of unequal character pairs depends on the characters. If the unequal pair of characters are non-alphabetic characters, the characters are ranked according to their numeric ASCII code equivalents. The string contributing the higher numeric ASCII code equivalent is considered to be greater than the other string. If the unequal pair of characters are letters of the alphabet, the pair is ranked in strict alphabetical order.

If one of the characters is a letter of the alphabet and the other character is non-alphabetic, the characters are ranked according to the numeric ASCII value of the uppercase letter, even if the letter is in lowercase, and the numeric ASCII value of the non-alphabetic character.

Arrays and Relational Expressions

Elements of dimensioned and dynamic arrays can be compared in relational expressions using the standard syntax. In addition, all elements of the array can be compared to a specified value by using an asterisk (*) to identify the element. In multi-dimensioned arrays, one or more dimensions can be specified using the example.

When the asterisk is used, the operators < > = <= >= # return a true value if at least one element of the array satisfies the relation. The asterisk may be specified only in relational expressions and only in one array expression in a statement.

Match - Pattern Matching Operator

string.variable match pattern

The match operator compares a string value to a predefined pattern and evaluates to 1 (true) or 0 (false). The pattern may consist of any combination of the following:

pattern operation
nn tests for n integers.
na tests for n alphabetic characters.
nx tests for n characters of any type.
'string' tests for literal string (double quotes are used when testing for a literal string in combination with the above patterns).

If n is 0, the relation evaluates to true only if all the characters in the string conform to the specified data type. The character n tests only for integers; +, -, and . are not considered to be numeric.

Logical Expressions

Logical expressions (also called Boolean expressions) are the result of applying logical (Boolean) operators to relational or arithmetic expressions.

Logical operators work on the true or false results of relational or arithmetic expressions. (Expressions are considered false when equal to zero, and are considered true when non-zero.)

Logical operators have the lowest precedence and are evaluated after all other operations have been evaluated. If two or more logical operators appear in an expression, the leftmost is performed first.

Logical operators act on their associated operands as follows:

a OR b is true (evaluates to 1) if a is true or b is true.

a ! b is false (evaluates to 0) if a and b are both false.

a AND bis true (evaluates to 1) only if both a and b are true.

a & b is false (evaluates to 0) if a is false or b is false or both are false.

Retrival and Update Lock Codes

If a Pick/BASIC program is executed by a user who does not have retrieval and/or update privileges for a file that does and is opened in the program, and then attempts to read and/or write to such a file, this will result in termination of the program with an error message. In order to read or write to such a file, users must make sure that the retrieval and update lock codes match those of the file that is opened. This is a system function and cannot be accomplished from Pick/BASIC. If the file is retrieval protected, the file cannot be opened and the program will be terminated with an error message; see the Security Section.

Pick/BASIC Statements

The following is a list of Pick/BASIC statements. For more detailed information about these statements, refer to their individual entries in the body of Advanced Pick Reference Manual.

* ! abort
assigned aux begin
block break
call capturing casing
chain chap (6.2) clear
clearfile clearselect (6.2) close
common convert crt
data debug del
delete dim echo off
echo on else end
end case enter equ{ate}
execute exit file
footing for.. next
for next .. until for next .. while get
gosub go{to} heading
hush (6.2) if..then..else in
{$}include input inputclear
inputparity (6.2) inputnull (6.2)
inputtrap ins insert
key let locate
lock locked log
loop loop ..until loop ..while
mat matbuild matparse
matread{u} matwrite{u} next
null on gosub on goto
onerr open out
page precision print
print on printer close printer off
printer on procread procwrite
program prompt read{u}
readnext readt readtl
readtx readv{u} release
rem repeat replace
return returning return to
rewind root rqm
select send sleep
setting shift spoolq (6.2)
stop
subroutine tcl tclread
then..else unlock waiting
weof write{u} writet
writev{u} unblock (6.2)

Pick/BASIC Functions

The following is a list of Pick/BASIC functions. For more detailed information about these functions, refer to their individual entries.

@ functions % functions abs
access alpha ascii
assigned change (6.2) char
col1 col2 convert
cos count date
dcount delete dquote (6.2)
dtx ebcdic ereplace (6.2)
error exchange (6.2) exp
extract field fmt (6.2)
fold iconv index
insert inmat (6.2) int
len ln maximum (6.2)
mod not num
occurs oconv pwr
rem replace rnd
scan (6.2) sentence (6.2) seq
sin sort soundex
space spooler (6.2) squote (6.2)
sqrt str summation (6.2)
sum swap (6.2) system
tan time timedate
trim xtd

Pick/BASIC Symbolic Debugger

The Pick/BASIC Symbolic Debugger facilitates the debugging of new Pick/BASIC programs and the maintenance of existing Pick/BASIC programs. The Pick/BASIC debugger requires sys2 privileges and has the following general capabilities:

- Step through execution of program in single or multiple steps.
- Transfer to a specified line number.
- Break execution at specified line numbers or when specified logical conditions have been satisfied.
- Display and/or change any variables, including dimensioned variables.
- Trace variables.
- Enter the system debugger.
- Direct output to either terminal or printer.
- Display and/or pop GOSUB stack.
- Display source code lines.

The Pick/BASIC debugger may be entered at execution time under the following conditions:

- The <break> key is pressed.
- The d (debug) option is specified with the run verb.
- The debug statement is executed in the program.
- A runtime error is encountered (unless the a (abort) option of the run verb is selected).
- The abort statement is executed in the program.
When the Pick/BASIC debugger is entered, it indicates the source code line number to be executed next and prompts for commands with an asterisk (*).

Symbol Table

When a Pick/BASIC program is compiled, a symbol table is generated, unless the s (suppress table) option has been used. The symbol table is used by the Pick/BASIC debugger to reference symbolic variables.
If a program calls an external subroutine, and the Pick/BASIC debugger has been entered previously, a complete symbol table is set up for the external subroutine. Break points set up for a subroutine are independent from break points set up in the main program or other subroutines; however, the execution counters e and n are global. That is, the break point counters count both main program and subroutine break points in the order they are encountered. Multiple symbol tables allow programmers to set up different break points and/or variable traces for different subroutines.

Pick/BASIC debugger commands are listed below. For more detailed information about each command, refer to it's entry.

b Establish a break point condition.
c Toggles source line display on and off.
d Display breakpoint and trace tables.
debug Escape to system debugger.
e Execute a specified number of lines.
end End program execution and return to TCL.
g Begin program execution at a specified line.
j Executes next line, displays next line if "c" option enabled.
k Remove a specified breakpoint.
l Display specified source lines.
l* Display all lines.
lp Toggle output device between terminal and printer.
n Execute through a specified number of breakpoints.
off Logs the user off the system.
p Inhibit display of program output.
pc Close output to spooler.
r Pop the top element off the gosub stack.
s Display the gosub stack.
t Set trace for a specified variable .
u Remove a specified trace.
$ Verify object code.
v Verify object code.
/var Print value of a specified variable.
/arr(m) Print value of a specified array element.
/* Print value of entire symbol table.
[] Display specified number of characters.
? Show the current program name, line number, and verify the program.

The following debugger commands are available from FlashBASIC:

down Move down the command stack.
edit Use the Update processor to edit the current program.
help Show the debugger commands.
up Move up the command stack.
?! show the current command stack and parameters.
Syntax
Options
Example
Purpose
Related