Strings in G7

Strings may be defined and referenced by name. These same tools may be used to open an arbitrary text file and parse the content found there.

str <name> = <rhs>
str <name> += <rhs>
Create a string named <name> with definition <rhs>. If the string <name> exists already, then <rhs> may be appended with use of the ‘+=’ operator. The string definition <rhs> may be specified as text presented within quotation marks, given as the name of a previously-defined string, or defined as a sequence of text and/or strings linked by ‘+’.

For example,

str concept  = "Output"
str industry = "Coal Mining"
str display  = concept + "of the " + industry + " industry."
str save <filename>

Store all defined strings in a text file <filename>. Afterward, the strings may be loaded into memory again.

For example,

str save MYSTRINGS.TXT
add MYSTRINGS.TXT
str store args <rootname>

Store all add-file or function arguments that are in scope in a series of strings. Each string name is created from the specified root name and the position (integer) of the argument in the argument list (e.g. root1, root2, …).

For example,

# Initialize a collection of series from a list of series names. Create new series or set existing series to zero.
function Initialize{
   str store args a
   do{ f %s( a%1 ) = 0 }(1 - %NARGS)
   }
Initialize gdp consumption investment
str print

Print each string to the screen.

str clear [<stringname>]

Erase all defined strings from memory. If a string name is given, then that string will be removed from the list of user-defined strings.

str replace [<stringname>] <”search_text”> <”replacement_text”>

This function operates on the string <stringname>, if provided, or the last line read by “str getline”. Any text in this string matched by the string <search_text> is replaced by the string <replacement_text>. Matching employs regular expressions as defined in the C++ Boost library. More details for regular expressions are provided on the following page.

The following routines are employed in the parsing of a text file.

str open <filename>

Open a file for parsing with the string .

str close

Close the file that was opened with the str open .

str getline [<string_name>]

Read a line of text from the file opened by str open. Store the text as a string named <string_name>. Store a copy of the string with the name “line;” this string may be referenced by related routines, but it does not appear in the list of user-defined strings.

str parse [<string>] [“<separators>” [“<string_name>”]]

Split the string named <string> into words separated by any one of the characters listed as separators. If no string name is provided, then the routine acts on the last line read by str getline. The default separators list is composed of the space and tab characters, or “ \t”. The words are stored as a list of strings that may be accessed with the %w() function, which has a syntax similar to the %s() function described below. If a string name is suppied, it will be used as the root name of the stored words.

Several keywords and functions aid in the parsing of text files.

%linelen

Returns the length of the last line read by str getline. If str getline encounters the end of the file, then a value of -1 is returned by %linelen.

%line

The %line keyword allows the user to access the string last read by str getline. It especially is useful when no string name was provided for the string.

%numwords

The %numwords keyword returns the number of words parsed from the line currently in memory or the string last processed by the str parse . After calling str getline, the number of words is 1 (%line == %w(1) and %numwords == 1) until a subsequent str parse is issued.

%w(<index> [,<first>[,<last>]])

The %w() function provides access to the list of words created by the str parse routine. <index> is an integer that specifies the position of the desired string within the list of words created by str parse. The optional <first> and <last> parameters are integers that specify the positions of the first and last characters to define a substring of the word in position index.

The definition of a string may be recovered at nearly any point within a G7 script by using the %s(<name>) function function. To employ our example string “display”, which is defined as “Output of the Coal Mining industry.”, as a graph title, we simply issue the following .

gr %s( display )

Additional functions are available function that compare strings, calculate the length of the string, and perform other operations.