Prev: help.html Up Next: array.html
The universal variable type: var

Practical Guide:

A var type mimics perl's variables: it is simultaneously a string and a floating point number, and can be accessed or modified as a string or a floating point number: the operators & and &= modify it as a string, the usual arithmetic operators act on them as a floating-point number.

var v = 1.23;
v += 1;               // increase by 1
v &= 3;               // append the string representation of 3
                      // the value of 'v' is now: 2.233
var x = "hello" & v;  // the value of 'x' is: hello2.233

double d = v.dbl();   // explicit conversions
string s = v.str();  
int    i = v.integer(); 

x(1,3) = "--";        // substrings are accessed with the () operator
                      // the value of 'x' is now: "h--o2.233"

What is a var?
This class tries to emulate the behavior of Perl's variables: it provides a smooth transition between numerical and string variables, combining the two in one type. It can be accessed (read or modified) both as a numerical or as a string variable, and in the future it will provide all member functions of std::string as well, and hopefully regular expressions (for searching, replacing) will be supported as well. This is the basic variable type in blop, graphs store their contents in this form as well, making it possible to access the contents of data files (and represent them on the plots) as string as well, not only as numerical data.

Arithmetic operations
The following operators (well known from C++) treat a var as a numerical value, and operate on them accordingly:
++,-- Increment (decrement) the value by 1. These operators keep zero-paddedness, i.e. if a var is an integer (contains only digits after an optional leading -), and has a value '001', and one calls the ++ operator on this, it will be '002'
+=,-= Increment (decrement) by a given value, which can be a string (it will be converted to a numerical value in this case), a var, a double or an int. In the last case (and if the var is an integer also) these operations keep zero-paddedness. That is, if '08' is incremented by 1, it will be '09'. Keep in mind, that += and -= called sequentially with the same value do not necessarily have zero effect: if '09' is incremented by 2 and then decremented by 2, it will be '9', instead of '09'
var a = "001"; 
a += 12;       // a has the value '013'
a += 1000;     // a has the value '1013' now
*=,/= Multiply or divide by a given value, which can be a string, var, double or int.
*,/,+,- These operators can be called with a var on the left or right (or both) side. The other operand can be a var, string, double or int. The result is a var

The >, <, >=, <= operators compare their arguments as floating point values. To compare a var as a string, convert it to a string (so that the string comparison operator of STL is invoked):
var a = "apple";
if(a.str() < "orange") { ... }

The behaviour of the ==, != operators depends on their arguments: if a var is compared to another var, a string or a char*, it carries out string-comparison. Otherwise a floating point comparison is carried out.

var a = "001";
var b = "1";
if(a == b)             cerr<<"This will not be printed"<<endl;
if(a.dbl() == b.dbl()) cerr<<"This will be printed"<<endl;
(This is not necessarily a good approach, it might change in the future. To be sure what is happening, you can always convert it to string or double explicitely, using the dbl() or str() member functions)

String appending
The & operator (if at least one of its arguments is a var) concatenates its two arguments, returning a var. Similarly, the &= operator appends its right-hand argument to its right-hand argument (this must be a var):
var a = "hello ";
a &= "world";

Formatting of numerical values

Vars automatically convert between string and numerical representations. When they are set to a numerical value, the string representation is automatically updated using a sprintf(...) conversion. To control the format of this conversion, one can use the

format(const var &format);
member function. Any later assignment from a numerical value will use this format.

One can find-and-replace substrings in a var, using its member function. It modifies the original var:
var a = "Hello boy";
a.replace("o","a");  // replace all occurences of "o" to "a";  Result: Hella bay
a.replace1("l","d"); // replace first occurence of "l" to "d"; Result: Hedla bay
The following nom-member functions replace all occurences of from to to in the string s, and return the modified string (the original is left intact):
var replace(const var &from, const var &to, const var &in);
var replace1(const var &from, const var &to, const var &in);
var replace_re(const var &pattern, const var &replacement, const var &in );

A portion of a var can be accessed via the ( ) operator (parenthesis operator). This operator accepts 2 arguments, the index of the first and the last character of the substring. For example if s = "apple", then s(1,3) evaluates to "ppl". The returned substring behaves exactly like a normal var, but it is more: it contains a reference to the original var, of which it is a substring. Any modifications to this substring will effect the original var:
var s = "apple";
s(1,3) = "sk m";
Now 's' has the value "ask me"

Regular expression matching

There are two member functions to match regular expressions against this variable:

bool var::matches(const var &regexp);
This function returns true, if the regular expression regexp matches. It is taken to be a POSIX extended regular expression.
int var::matches(const var &regexp, array &results);
This function also returns the matching parts of the string, and returns the number of matches (the size of the array results). If this is not zero, the first (index-0) element of this array is the part of the string matching the whole regexp, subsequent elements are the parts matching the subexpressions (contained within braces: ()) For example matching the pattern "(l.).*(l.)" against the string "Hello world" returns: results[0]="llo world", results[1]="ll", results[2]="ld"
These functions use the POSIX regex functions from the c library. See man regex or man 7 regex to see more about them.

Regular expression replacement

The following member-function searches a match of the regular expression 'pattern', and if this is found, replaces it to the text 'replacement'.

var::replace(const var &pattern, const var &replacement);
This replacement string may contain the strings \0, \1, \2, etc. \0 refers to the whole matching expression, \1, \2, etc refer to the 1st, 2nd etc sub-expressions. Do not forget that you need to escape the backslash, for example: s.replace_re("_([0-9])_","\\1");

... more coming soon

Source files:

Prev: help.html Up Next: array.html