String variables

String variables can be used wherever strings are expected, such as file names and keyword parameters.

A string is defined to be a one dimensional array of ASCII characters. A string can be a literal quote string, such as, 'this is a quote string', or an expression that results in a string, such as RCHAR(35.6).

A literal quote string may begin with the opening quote, ` and end with the single quote, ', or may begin and end with the single quote, or may begin and end with the double quote, ".

Hint The simplest way to include a single quote in a string is to use double quotes to delimit the string, e.g., "ABC'DEF". Similarly, to include a double quote in a string, delimit the string with single quotes, e.g., 'ABC"DEF'. Another method is to use the CHAR function to convert the ASCII code for a quote into a character and append it to the string using the append operator, //. For example: 'ABC'//CHAR(34)//'DEF', since the ASCII code for " is 34. The ASCII codes for the standard characters can be found using the ICHAR function.

A string variable is a single string, that is, a one dimensional array of characters. A string array variable is an array of strings. An element of a string variable is a single character. An element of a string array variable is a string. There is no maximum length for either a string variable or any element of a string array variable, nor a maximum number of elements of a string array variable. The elements of a string array variable need not be the same length.

The CLEN function returns a scalar value equal to the length of a string.

A string variable, or an element of a string array variable, can be entered directly by means of an assignment. For example:

 TS='This is a string'    ! string variable
 TA[3]='This is a string' ! array string variable: third element
 

An entire string array variable can be created with the READ/TEXT command.

Commands that expect strings, such as the TEXT command, which draws a string, or the WRITE command, which expects a file name as a parameter, will only accept a single string. Remember, though, that a string can be a literal quote string, a string variable, one element of a string array variable, and/or some combination of string functions and string operators.

The following table shows all of the possible ways that a string variable can be considered to be equivalent to a single string, that is, can be used wherever a string is expected.

Let a be a scalar and let x be a vector
Suppose that TA is a string array variable and T is a string variable
T= T[i] for i = 1, ..., CLEN(T)
TA[a]= TA[a][i] for i = 1, ..., CLEN(TA[a])
T[x]= T[i] for i = x[1], x[2], ..., x[#]
TA[a][x]= TA[a][i] for i = x[1], x[2], ..., x[#]

Strings may be appended together using the append operator, //. For example, suppose that T is a string variable with the value 'this is a string'. You can make a new string variable using the assignment:

T2='start of new string '//T//' end of new string'

and T2 will have the value: 'start of new string this is a string end of new string'.

A variable name can be converted to a string by means of the VARNAME function. A scalar value can be converted to a string by means of the RCHAR function. For example, if A is a scalar with the value -1.234, and T is a string variable with the value ' units', then the assignment:

T2='The value of '//VARNAME(A)//' is '//RCHAR(A)//T

makes a string variable T2 with the value: 'The value of A is -1.234 units'.

A format string is allowed as the second argument of the RCHAR function. For example:

T2='The value of '//VARNAME(A)//' is '//RCHAR(A,'F4.1')//T

makes a string variable T2 with the value: 'The value of A is -1.2 units'.

Expression variables

String variables can be used in numeric expressions, as so called expression variables, to shorten or to simplify an expression. Parentheses around an expression variable are assumed during a numeric evaluation. For example:

 T='A+B'
 Y=X*T      ! this is equivalent to Y=X*(A+B)
 

A string variable will be numerically evaluated if it is a numeric operand or the argument of a numeric function. Otherwise, a string variable is treated as a string. You can force numeric evaluation by using the EVALUATE function. For example:

 T='3+2'        ! define T to be a string variable
 =T             ! the string '3+2' will be displayed
 =EVALUATE(T)   ! the numeric value 5 will be displayed
 

The EXPAND function produces a string by parsing the input string and expanding any expression variables present in this string. If an expression variable, contained in the original string, also contains expression variables, they are also expanded, and so on until all such expression variables have been expanded. Syntax checking is done during the expansion.

The maximum length of a completely expanded expression is two thousand five hundred (2500) characters.

As an example of expression variable use, consider the following set of instructions:

 A=2              ! define a scalar A
 B=3              ! define a scalar B
 FC1='(A+B)/A'    ! define a string variable FC1
 FC2='SQRT(A/B)'  ! define a string variable FC2
 FC3='FC1*FC2'    ! define a string variable FC3
 FC4='FC3+4*FC2'  ! define a string variable FC4
 =FC4             ! displays 'FC3+4*FC2'
 =EXPAND(FC4)     ! displays '(((A+B)/A)*(SQRT(A/B)))+4*(SQRT(A/B))'
 =EVALUATE(FC4)   ! displays 5.3073
 

  Matrix variables
  Making a vector