4 Data Variables and Data handling

String Variables

A string is a series of ASCII characters which can be stored in a string variable. (In Eloquence a string may also hold display enhancement and line drawing characters.) A string variable can be declared in a DIM or COM statement, which specifies the maximum length of the string. The maximum length of a string is the maximum number of characters it can hold. If a string variable is used without first being specified in a DIM or COM statement, it is implicitly dimensioned to be 18 characters maximum. The current length of a string refers to the number of characters currently contained within it.

Each string variable name is terminated with a dollar sign ($). For example:

X$

Home_address$

Part_no1$
Characters can be assigned to a string (or substring) variable using the LET statement:

[LET] string (or substring) variable [=string variable ... ]=string expression

For example:

LET Title$="Chapter 1"

Asterisks$="*********"

Married_name$=Husbands_name$="Smith"
There are many other ways to assign values, as shown later in this chapter.

String Arrays

A string array is equivalent to a numeric array except that its elements are strings. String array names follow the rules for string variable names. String arrays are dimensioned in a DIM or COM statement. Every string in the array has the same maximum length. Like a numeric array, a string array can be implicitly dimensioned. In all string operations, an element of a string array can be used like a simple string variable. The element is accessed in the same way as a numeric array--by quoting the array name and the subscript(s).

For example, the following statement prints the string contained in the quoted element of the three-dimensional string array Name_array$:

PRINT Name_array$(1,3,4)

String Array Defaults

Explicitly dimensioning a string array conserves memory if the amount of space required is less than the default (10 elements per subscript). As each default element contains an 18 character string, a lot of space may be wasted. Always dimension string arrays explicitly.

String Expressions

Text within quotes (a literal) is the simplest form of a string. Any ASCII character can appear within the quotes. If you want to print quotation marks themselves you must use string concatenation and the CHR$ string function. This is because the quotes are used as the literal delimiter. This will be dealt with fully in the next chapter. String expressions may contain any of the following:

As with numeric expressions, a string expression can be enclosed in parentheses if necessary.

Substrings and string concatenation are covered in this chapter. The next chapter covers the built-in string functions and shows how to define your own string functions.

Substrings

A substring is a portion of a string rather than the entire string. Normally, a reference to a string variable refers to the entire string. For instance, if Example_string$ = "ABCDEF" and the statement LET B$ = Example_string$ is executed, B$ = "ABCDEF". However, sometimes it is necessary to reference only a portion of a string. Suppose B$ is to be set equal only to the last three characters of Example_string$. This can be done using the substring designator. Again, assume that Example_string$ = "ABCDEF". This time execute the statement LET B$ = Example_string$[4,6]. The result is now B$ = "DEF", not "ABCDEF".

There are three ways to designate a substring. The first method is to indicate the starting index (position) of the substring, followed by a comma and the ending index of the substring. All indexes are inclusive. For example, if Example_string$ = "ABCDEFGHIJ", then:

Example string$[1,3] = "ABC"

Example_string$[4,6] = "DEF"

Example_string$[7,10] = "GHIJ"

Example_string$[1,10] = "ABCDEFGHIJ"
Notice that Example_string$[1,10] is effectively the same as Example_string$.

The second method of designating a substring is to give the position of the first character, followed by a semicolon and the length of the substring. Continuing the above example:

Example_string$[1;3] = "ABC"

Example_string$[4;3] = "DEF"

Example_string$[7;4] = "GHIJ"

Example_string$[1;10] = "ABCDEFGHIJ"
Here too, Example_string$[1;10] is effectively the same as Example_string$.

The third method of designating a substring is to give the starting index of the substring only. This is really a special case of the first method, in which the ending index is assumed to be the length of the string. Continuing the example:

Example_string$[1] = "ABCDEFGHIJ"

Example_string$[5] = "EFGHIJ"

Example_string$[10] = "J"
So Example_string$[1] is effectively the same as Example_string$.

Regardless of which method is used, the first position indicator, the second position indicator, and the length indicator may be any valid numeric expression.

NOTE: If the numeric expression is not a whole number, it will be rounded to the nearest integer.

Here are more examples:

Example_string$[N,M]

Example_string$[F;L]

Example_string$[N + SQR(Z);L-1] ! so long as  Z > 0!

Example_string$[A(I),A(J)]

The NULL String

The null string is a string without any characters at all, not even blanks or non-printing characters. It is obtained by entering two sets of quotes ("") or giving a substring length of 0. For example:

A$=Example_string$[1;0] ! Set A$ to the NULL string

Test$="A null"&A$&"string" !There will be no space between null and string

A nullstring
There is a special case, however, with a null substring. When a null substring is assigned to a string then the characters covered by that substring become blanks. The string length is not altered. For example, using our previous test:

Example_string$="ABCDEFGHIJ"
Example_string$[3;2]=""!Assign a null string to characters 3 and 4

AB  EFGHIJ
Characters 3 and 4 are changed to blanks and Example_string$ is still 10 characters long.

All strings are initialized to the null string by executing RUN, SCRATCH C, or SCRATCH V.

String Concatenation

The ampersand sign (&) is the string concatenation operator. It joins strings, substrings, and string expressions. No blanks are inserted between strings. Note the following example:

20 The$="the"
30 Example$="I am "&The$&" "&CHR$(38)&" sign."
40 PRINT Example$
I am the & sign

Examples of String Use

The following example shows substrings in use. Substrings are frequently used to insert or change characters in a string without affecting the rest of that string. Suppose a university wished to print students' results. Every letter heading would be the same; the only change would be in the faculty name.

10   DIM Faculty_title$[22]
The string Faculty_title$ has maximum length of 22 characters.

20   Faculty_title$="School of "     Initially, Factualty_title is a  
30   LET B$="Business"         10 character string "School of " 50   En$="Engineering"               (   indicates a blank space)
60   L$="Law"
70   Bw$="Basket Weaving"
80   C$="Civil"
100  Faculty_title$[11]=B$
The string B$ is inserted into the string Faculty_title$ beginning at the 11th character of Faculty_title$.


130  PRINT Faculty_title$             Prints: School of Business
170  Faculty_title$[11]=En$

210  PRINT Faculty_title$             Prints: School of Business
If single subscripts are used when assigning substrings (as in the examples above) and the item being assigned is too short to fill the string, then the rest of the receiving string is truncated. Thus, continuing the example, when the string L$ is inserted at character 11 in Faculty_title$, the rest of Faculty_title$ is erased:

250  Faculty_title$[11]=L$
290  PRINT Faculty_title$             Prints: School of Law
If single subscripts are used and the item being assigned is too long for the substring, an error is returned:

420  Faculty_title$[11]=Bw$
        ERROR 18 IN LINE 420
An attempt to insert the 14 chars of Bw$ in the 11 remaining chars of Faculty_title$ returns ERROR 18.

If two subscripts are used when assigning substrings, then the changes will only be made in the range of the receiving string specified by the subscripts. The length of the receiving string is retained and any receiving string characters outside the subscript range will be unaffected. To continue the example:


450 Faculty_title$[11,22]=B$L$
460 PRINT Faculty_title$            Prints: School of BusinessLaw
Note that the concatenation operator (&) does not insert a blank. A better title would be printed by:

500 Faculty_title$[11,22]=B$&" "L$
510 PRINT Faculty_title$            Prints: School of Business Law
Now for the Civil Law faculty. Note that if the substring to be added is too short, the rest of the receiving string, within the range specified by the two subscripts, is filled with blanks:

450  Faculty_title$[11,18]=C$
460  PRINT Faculty_title$           Prints: School of Civil    Law
If the substring to be added is too long, it is truncated to fit in the length specified by the subscripts--no error is indicated.

590 Faculty_title$[11;12]=Bw$
600 PRINT Faculty_title$            Prints: School of Basket Weavi
Here is a summary of substring errors and their error codes:

Columns
Invalid Designator Action
Starting index < 1Error 18
Ending index < 0Error 18
Starting index > ending index + 1Error 18
Starting index = ending index + 1Null string
String length = 0Null string
Starting index > current length of string + 1Error 18
Ending index > dimensioned length of stringError 18


Eloquence Language Manual - 19 DEC 2002