Variables

You can define variables in command files and do calculations on variables in expressions or in GEDCOM expressions.

Defining Variables

Variables are defined and used in the form #varname where #varname is any valid variable name. The rules for a valid variable name are:

A variable is initially defined in an assignment statement such as:

#x=1

where the variable is set equal to the right side of the equals sign which can be any valid expression.

Even though variables in Internal Scripts need not start with '#', using number signs is highly recommended. This approach guarantees that variable and object names are never the same (because object names cannot start in '#'). Furthermore, some scripting commands expect variable names by references (i.e., quoted variable name) and those variable names are required to start in '#'. An additional benefit of using '#' is that the GEDitCOM Editor will color code variable names in editing windows.

Variable Arrays

Any valid variable can be a variable array by following it with an expression in square brackets

#y[#j]=1

where

Accessing Variables and Variable Arrays By Reference

Variables can be set and accessed by reference (since 3.5/1.0). The process is similar to other languages such as accessing variables in C++ using pointers. Here, a reference is a string with the name for the variable. A reference can set a variable as follows:

#yref="#y"
*#yref=3+5
write #y       ! output is 8

The variable #yref is set to a string equal to another variable's name making it a "reference" to that variable. The '*' before #yref means to set the variable referred to by #yref to the expression on the right. The reference can be an array variable including one with an expression for its index (e.g., #ref="#x[#i]").

Setting a variable using *#yref is identical to setting variables in C++ using pointers, but unlike C++, you cannot use *#yref in an expression to mean "value of the referenced variable." Instead, you can get value of a referenced variable using the val(ref) function. For example

#y=2
*#yref = val(#yref)+2
write #y       ! output is 4

Working with variable references is normally only needed when writing subroutines that need to change variable values.

Notes

  1. Both assignment command or an assignment by reference can be combined with an operator as in
    #x [op] = [EXPRESSION]
    *#xref [op] = [EXPRESSION]
    
    which are equivalent to
    #x = #x [op] ([EXPRESSION])
    *#xref = val(#xref) [op] ([EXPRESSION])
    
    provided the variable is already a defined variable. It is an error if the variable has not previously been defined. See expression help information for the possible operators.
  2. Occasionally a script needs to remove a defined variable, which can be done with an Unset command.