As has already been hinted at in Section 2.3, PyXPlot recognises two types of variables: numeric variables and string variables. The former can be assigned using any valid mathematical expression. For example:
a = 5.2 * sqrt(64)
would assign the value 41.6 to the variable a. Numerical variables can subsequently be used in mathematical expressions themselves, for example:
a=2*pi plot [0:1] sin(a*x)
String variables can be assigned in an analogous manner, by enclosing the string in quotation marks. They can then be used wherever a quoted string could be used, for example as a filename or a plot title, as in:
plotname = "The Growth of a Rabbit Population" set title plotname
String variables can be modified using the search-and-replace string operator1, =, which takes a regular expression with a syntax similar to that expected by the shell command sed and applies it to the relevant string.2 For example:
twister="seven silver soda syphons" twister =~ s/s/th/ print twister
Note that only the s (substitute) command of sed is implemented in PyXPlot. Any character can be used in place of the / characters in the above example, for example:
twister =~ s@s@th@
Flags can be passed, as in sed or perl, for example:
twister =~ s@s@th@g
Table 3.1 lists all of the regular expression flags recognised by the = operator.
g |
Replace all matches of the pattern; by default, only the first match is replaced. |
i |
Perform case-insensitive matching, such that expressions like [A-Z] will match lowercase letters, too. |
l |
Make |
m |
When specified, the pattern character |
s |
Make the . special character match any character at all, including a newline; without this flag, . will match anything except a newline. |
u |
Make |
x |
This flag allows the user to write regular expressions that look nicer. Whitespace within the pattern is ignored, except when in a character class or preceded by an unescaped backslash. When a line contains a #, neither in a character class or preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored. |
Strings may also be put together using the string substitution operator, %, which works in a similar fashion to Python string substitution operator. This is described in detail in Section 2.3. For example, to concatenate the two strings contained in variables a and b into variable c one would run:
c = "%s%s"%(a,b)
One common practical application of these string operators is to label plots with the title of the data file being plotted, as in:
filename="data_file.dat" title="A plot of the data in {\tt %s}."%(filename) title=~s/_/\_/g # Underscore is a reserved character in LaTeX set title title plot filename
Footnotes