I. Variables and Tables

What is a variable?

In the Z-Tree scripting language a variable is a named reference to a value stored in a table.  Simply said, a variable is a name assigned to a value for the purposes of its storage, retrieval, and manipulation. The name of each variable is listed at the top of each table with the corresponding values assigned to it below.

What is a Table?

Tables are the data structures all variables are stored in. Tables in Z-Tree are analogous to tables in a spread-sheet in that the variable names are placed at the top of each table column with their corresponding values listed below. The table a variable will be stored in is determined by the table scope in which it is declared within.

The three most important tables Z-Tree provides by default are the Globals, Subjects, Contracts, Summary, Session, and Logfile tables.

Note: Variables in Z-Tree are stored as floating point numbers  (approximation of a rational number), and cannon be explicitly used to store text (strings) — with one exception (see Chat Box).

Variable Declaration

 Syntax

ExampleTable.do {
    variable_name = value;
}

Variable variable_name is declared in the SyntaxExample table scope, and is initialized to the value value.  Since var is declared within the Subjects scope it will be placed in the Subjects table.

Examples

Ex 1:

Globals.do {
    show_up_fee = 10;           
}

example 1
In example one, a variable show_up_fee is declared within the Global table scope and initialized to the integer value 10. Since the variable show_up_fee was declared within the Globals scope it will be stored in the Globals table, as depicted in the above illustrated.

Ex 2:

Subjects.do {
    initial_endowment = 5.50;     
}

In example 2 the variable initial_endowment is declared within the Subjects table scope and initialized with the rational value 5.50. Since the variable initial_endowment was declared within the Subjects scope it will be stored in the Subjects table. The illustration above provides a snap-shot of the state of the Subjects table immediately after the initial_endowment variable is set to 5.50. Note: there are two Subjects, one for each row. As each subject executes the initialization above each will also have their own initial_endowment.

 

Naming Variables

Properly naming variables greatly impacts program readability. Variable names should simply describe the entity they represent. Avoid using short cryptic variable names such as: e, deltax, y, z, x, x1, x2.

Poor Variable Naming

Subjects.do {
    r = end - b;     
}

In this fabricated situation a subject has bid the most for an item and has won the auction. The above
calculation subtracts the subjects highest bid from her initial endowment, storing the remaining funds
in the variable r.

Improved Variable Naming

Subjects.do {
    remaining_endowment = initial_endowment - highest_bid;     
}

Once the variable names have been changed to better describe their respective entities the operation becomes much easier to read.
 

Avoid Enumerated Variables

Globals.do {
    payoff_1 = rand();
    payoff_2 = rand();
    payoff_4 = rand();
    payoff_5 = rand();
    payoff_6 = rand();
    payoff_7 = rand();
    payoff_8 = rand();
    payoff_9 = rand();
    payoff_10 = rand();
    payoff_11 = rand();
    payoff_12 = rand();
    payoff_13 = rand();
    payoff_15 = rand();
}

In the above example fifteen random payoffs are created and placed in the globals table. At first glance this may seem appropriate. Yet, the limitation of this implementation can be gleaned when a variable number of payoffs are required. Furthermore, if an operation needs to be performed on the entire collection of payoffs each payoff must be referenced manually — which from a readability standpoint furthers entropy.

As a general rule of thumb, if you find yourself creating an enumerated list of related variables use an array instead.

See the following section for more on arrays.

Next > Arrays and Matrices