3.7 Variables

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, =$\sim $, 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 =$\sim $ 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 $\backslash $w, $\backslash $W, $\backslash $b, $\backslash $B, $\backslash $s and $\backslash $S dependent on the current locale.

m

When specified, the pattern character \^{} matches the beginning of the string and the beginning of each line immediately following each newline. The pattern character $ matches at the end of the string and the end of each line immediately preceding each newline. By default, \^{} matches only the beginning of the string, and $ only the end of the string and immediately before the newline, if present, at the end of the string.

s

Make the . special character match any character at all, including a newline; without this flag, . will match anything except a newline.

u

Make $\backslash $w, $\backslash $W, $\backslash $b, $\backslash $B, $\backslash $s and $\backslash $S dependent on the Unicode character properties database.

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.

Table 3.1: A list of the flags accepted by the =$\sim $ operator. Most are rarely used, but the g flag is very useful. This table is adapted from Guido van Rossum’s Python Library Reference: http://docs.python.org/lib/node46.html.

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

  1. Programmers with experience of perl will recognise this syntax.
  2. Regular expression syntax is a massive subject, and is beyond the scope of this manual. The official GNU documentation for the sed command is heavy reading, but there are many more accessible tutorials on the web.