1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
<HTML>
<HEAD>
<TITLE>String variables</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<P><A NAME="variablestring"></A>
<font size="+2" color="green">String variables</font></P>
<P>
String variables can be used wherever strings are expected, such
as file names and keyword parameters.</P>
<P>
A string is defined to be a one dimensional array of ASCII
characters. A string can be a literal quote string, such as,
<code>'this is a quote string'</code>, or an expression that results
in a string, such as <code>RCHAR(35.6)</code>.</P>
<P>
A literal quote string may begin with the opening quote, <CODE>`</CODE> and
end with the single quote, <CODE>'</CODE>, or may begin
and end with the single quote, or may begin and end with the double quote,
<CODE>"</CODE>.</P>
<p>
<table>
<tr>
<td valign="top"><em><font color="red">Hint</font></em></a></td>
<td>The simplest way to include a single quote in a string is to use double
quotes to delimit the string, e.g., <CODE>"ABC'DEF"</CODE>. Similarly, to include
a double quote in a string, delimit the string with single quotes, e.g.,
<CODE>'ABC"DEF'</CODE>. Another method is to use the
<CODE><a href="../Functions/char.htm">CHAR</a></CODE>
function to convert the ASCII code for a quote into a character and append it to the
string using the <a href="../Operators/append.htm">append</a> operator,
<CODE>//</CODE>. For example: <CODE>'ABC'//CHAR(34)//'DEF'</CODE>,
since the ASCII code for <CODE>"</CODE> is <CODE>34</CODE>. The ASCII codes for
the standard characters can be found using the <CODE>
<a href="../Functions/ichar.htm">ICHAR</a></CODE> function.</td>
</tr></table></p>
<P>
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.</P>
<P>
The <a href="../Functions/clen.htm"><CODE>CLEN</CODE></a> function returns
a scalar value equal to the length of a string.</P>
<P>
A string variable, or an element of a string array variable, can be entered
directly by means of an assignment. For example:</P>
<P>
<font color="blue"><PRE>
TS='This is a string' ! string variable
TA[3]='This is a string' ! array string variable: third element
</PRE></font>
<P>
An entire string array variable can be created with the
<a href="../Commands/Read.htm"><CODE>READ/TEXT</CODE></a> command.</P>
<P>
Commands that expect strings, such as the
<a href="../Commands/Text.htm"><CODE>TEXT</CODE></a> command, which draws a string, or the
<a href="../Commands/Write.htm"><CODE>WRITE</CODE></a> 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,
<EM>one</EM> element of a string array variable, and/or some
combination of string functions and string operators.</P>
<P>
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.</P>
<p>
Let <code>a</code> be a scalar and let <code>x</code> be a vector.
Suppose that <code>TA</code> is a string array variable and <code>T</code> is a string variable.</p>
<p>
<center>
<table cellpadding="2" cols="4">
<tr>
<td valign="top"><code>T</code></td><td valign="top">=</td>
<td valign="top"><code>T[i]</code></td>
<td valign="top">for <code>i = 1, ..., CLEN(T)</code></td>
</tr><tr>
<td valign="top"><code>TA[a]</code></td><td valign="top">=</td>
<td valign="top"><code>TA[a][i]</code></td>
<td valign="top">for <code>i = 1, ..., CLEN(TA[a])</code></td>
</tr><tr>
<td valign="top"><code>T[x]</code></td><td valign="top">=</td>
<td valign="top"><code>T[i]</code></td>
<td valign="top">for <code>i = x[1], x[2], ..., x[#]</code></td>
</tr><tr>
<td valign="top"><code>TA[a][x]</code></td><td valign="top">=</td>
<td valign="top"><code>TA[a][i]</code></td>
<td valign="top">for <code>i = x[1], x[2], ..., x[#]</code></td>
</table></center></P>
<P>
Strings may be appended together using the <a href="../Operators/append.htm">append</a> operator,
<CODE>//</CODE></a>. For example, suppose that <code>T</code> is a string variable with the value
<code>'this is a string'</code>. You can make a new string variable using the assignment:</P>
<P>
<code>T2='start of new string '//T//' end of new string'</code></P>
<P>
and <code>T2</code> will have the value:
<code>'start of new string this is a string end of new string'</code>.</P>
<P>
A variable name can be converted to a string by means of the
<a href="../Functions/varname.htm"><code>VARNAME</code></a> function. A
scalar <EM>value</EM> can be converted to a string by means of the
<a href="../Functions/rchar.htm"><code>RCHAR</code></a> function. For example,
if <CODE>A</CODE> is a scalar with the value <CODE>-1.234</CODE>, and <CODE>T</CODE> is a string
variable with the value <code>' units'</code>, then the assignment:</P>
<P>
<code>T2='The value of '//VARNAME(A)//' is '//RCHAR(A)//T</code></P>
<P>
makes a string variable <CODE>T2</CODE> with the value:
<code>'The value of A is -1.234 units'</code>.</P>
<P>
A format string is allowed as the second
argument of the <code>RCHAR</code> function. For example:</P>
<P>
<code>T2='The value of '//VARNAME(A)//' is '//RCHAR(A,'F4.1')//T</code></P>
<P>
makes a string variable <CODE>T2</CODE> with the value:
<code>'The value of A is -1.2 units'</code>.</P>
<P>
<a name="expression"></a>
<font size=+1 color="green">Expression variables</font></P>
<P>
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:</P>
<P>
<font color="blue"><PRE>
T='A+B'
Y=X*T ! this is equivalent to Y=X*(A+B)
</PRE></font>
<P>
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
<a href="../Functions/eval.htm"><CODE>EVALUATE</CODE></a>
function. For example:</P>
<P>
<font color="blue"><PRE>
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
</PRE></font>
<P>
The <a href="../Functions/expand.htm"><CODE>EXPAND</CODE></a>
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.</P>
<P>
The maximum length of a completely expanded expression is two thousand five
hundred (<CODE>2500</CODE>) characters.</P>
<P>
As an example of expression variable use, consider the following set of
instructions:</P>
<P>
<font color="blue"><PRE>
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
</PRE></font></P>
<P>
<a href="VariablesS04.htm"><img src="../shadow_left.gif">
<font size="+1" color="olive">Matrix variables</font></a><br />
<a href="VariablesS06.htm"><img src="../shadow_right.gif">
<font size="+1" color="olive">Making a vector</font></a>
</P>
</BODY>
</HTML>
|