The class is designed for working with tables.
The table is considered defined (def
) if it isn't empty. The numeric value equals the amount of the rows in the table.
^table::create{table_data} ^table::create[nameless]{table_data}
The constructor creates an object of class table
, using table data provided in the constructor itself.
Table data
data provided in tab-delimited format, that isthe columns are separated by tab character, while rows are separated with a new-line character. Here, the parts of the first rowdivided by the tabulationare regarded as column names and thus a named table is created. Blank lines are ignored. If you want to create a table without column names (which is actually not recommended), you should precede table data with option nameless
. In this case, the constructor regards the columns of the first row as table data while their ordinal numbers (starting with zero) will be used as column names.
Example:
$tab[^table::create{name age Bob 27 Alex 22} ]
A new object of class table
named tab
will be created, containing two rows with column names defined as name
and age
.
^table::load[path] ^table::load[nameless;path]
The constructor creates an object using the table stored in a file. The data in the file must be provided in tab-delimited format (see also table::create
)
path
path to a file
The usage of option nameless
is the same as in constructor table::create
.
Example:
$loaded_table[^table::load[/addresses.cfg]]
The code given in example creates an object of class table
, containing named table stored in file addresses.cfg
located in the root directory of the website.
^table::sql{SQL-query} ^table::sql{SQL-query}[$.limit(n) $.offset(o)]
The constructor creates an object of class table
containing table based on data retrieved from a database. To use this constructor you must have connection with data server active (see operator connect
).
SQL-query
query sent to a database.
It is possible to use additional parameters of the constructor:
$.limit(n)
retrieve no more than n
entries
$.offset(o)
ignore first 0
retrieved entries
Example:
$sql_table[^table::sql{select * from news}]
The code will result in an object containing all data from table news
.
$table_name.column_name
Returns data from a specified column in current table row.
Example:
$tab.name
Example will return value stored in column name
of the current table row.
$table_name.fields
data stored in the current table row, returned as hash
Returns data stored in the current table row as hash. Here, the names of the columns are regarded as hash keys, while column data are regarded as respective values.
It is necessary to use this method if column names coincide with names of methods or constructors of class table
. In this case you cannot retrieve their values directlyParser will report it as error. If it is necessary to work with fields with such names, it is safe to use field fields
and work with not table but hash.
Example:
$tab[^table::create{menu line yes first no second} ] $tab_hash[$tab.fields] $tab_hash.menu $tab_hash.line
As a result, you will get values of fields menu
and line
(such names will coincide with methods of class table
) as keys of hash tab_hash
.
^table_name.save[path] ^table_name.save[nameless;path]
Saves a table in a text file in tab-delimited format. Using option nameless
will save table without column names.
Example:
^conf.save[/conf/old_conf.txt]
Table $conf
will be saved in text file old_conf.txt
in directory /conf/
.
^table_name.menu{code} ^table_name.menu{code}[separator] ^table_name.menu{code}{separator}
Method menu
executes code for each of the table rows, searching all table rows one by onein the given order.
Separator
is a code inserted after every non-empty row, except the last one. The separator
code given in square brackets is calculated only once, while that in the curly brackets is calculated every time it is called.
Example:
$goods[^table::create{pos good price 1 Monitor display 1000 2 System control unit 1500 3 Keyboard 15} ] <table border=1> ^goods.menu{ <tr> <td>$goods.pos</td> <td>$goods.good</td> <td>$goods.price</td> </tr> } </table>
The example outputs the entire content of the table $goods
as HTML-coded table.
^table_name.count[]
Returns the number of rows in the specified table (int
).
Example:
$goods[^table::create{pos good price 1 Monitor display 1000 2 System control unit 1500 3 Keyboard 15} ] Amount: ^goods.count[]
The example will output:
Amount: 3
In expressions, the numeric value of the table equals the amount of the rows:
^if($goods > 2){more}
^table_name.offset(number) ^table_name.offset[cur|set](number)
Moves current row offset specified number
of times down. If the numeric value of argument number
is negative, the offset is moved up. The offset displacement is made cyclicallythat is, having reached the last row in the table, the offset is displaced back up to the first row. Initial offset equals zero (0
).
Non-obligatory parameter:
cur
moves the offset with respect to the current row
set
moves the offset with respect to the first row
Example:
<table border="1"> ^goods.offset(-1) <tr> <td>$goods.pos</td> <td>$goods.good</td> <td>$goods.price</td> </tr> </table>
The given example will result in HTML-coded table containing the last row of the table from the previous example (i.e. given in the description of method menu
).
^table_name.offset[]
Method offset
, given without any parameters, returns the current row offset with respect to the beginning of the table.
Example:
$men[^table::create{name Jack Joe Roger }] ^men.menu{ ^men.offset[] - $men.name }[<br>] The code will return: 0 - Jack 1 - Joe 2 - Roger
Unlike the computer, human beings tend to count beginning with one (1
), not zero (0
). To make the output of numbered lists more comfortable for human understanding, you may use method line
:
^table_name.line[]
It allows us to get the offset in a more comprehensible mannerwhen the offset of the first row equals one (1
). If you use ^men.line[]
instead of ^men.offset[]
in the above example, the rows enumeration will start with one (1
) and end with three (3
).
^table_name.sort{sorting_function_by_string} ^table_name.sort{sorting_function_by_string}[sorting_direction] ^table_name.sort(sorting_function_by_number) ^table_name.sort(sorting_function_by_number)[sorting_direction]
The given method sorts the table according to the specified function.
sorting_function
is the function, whose current value determines the position of the row in the final (sorted) variant of the table. This value may be a string (values are compared in alphabetical order) or a number (values are compared as real numbers).
sorting_direction
determines sorting direction. The parameter may have two values:
desc
descending
asc
ascending
Ascending value is used by default.
Example:
$men[^table::create{name age Sergey 26 Alex 20 Mishka 29 }] ^men.sort{$men.name} ^men.menu{ $men.name: $men.age }[<br>]
As a result of this code, the rows of table $men
will be sorted by the values given in column name
:
Alex: 20 Mishka: 29 Sergey: 26
You may sort the table rows by the values given in column age
in descending order (desc
) if you substitute the line of the code which is calling method sort
for this one:
^men.sort($men.age)[desc]
The code will result in:
Mishka: 29 Sergey: 26 Alex: 20
^table_name.append{data}
The method appends data to the end of the table. The data
must be provided in tab-delimited format.
The table data must have the same structure as the table to which it is appended.
Example:
$stuff[^table::create{name pos Alexander boss Sergey coder }] ^stuff.append{Nikolay designer} ^stuff.save[stuff.txt]
The example code will append a new row to the table stuff
and save the whole table to file stuff.txt
^table1.join[table2]
The method joins table2
data to the end of table1
. Here, the method will retrieve from table2
the value placed in the column, whose name coincides with the name of the column in table1
or a blank line (if such column cannot be found). Columns in table2
may have no order or be missing.
Example:
^stuff.join[$just_hired_people]
All entries of table just_hired_people
will be joined to table stuff
.
^table_name.flip[]
The method creates a new nameless
table with entries resulted from flipping the table. That means, the method turns columns of the given table into rows and rows into columns.
Example:
$emergency[^table::create{id number fire 01 police 02 ambulance 03 gas 04 }] $flipped[^emergency.flip[]] ^flipped.save[flipped.txt]
As a result of the code, the following table will be saved as a file named flipped.txt
:
0 | 1 | 2 | 3 |
fire | police | ambulance | gas |
01 | 02 | 03 | 04 |
^table_name.locate[column_name;value_to_be_located]
The method locates a specified value in a specified column in the table and returns Boolean value "true/false" depending on whether it found the value or not. If it located the specified value, the row where the value was found is regarded as current. If the value was not located, the offset doesn't change.
While trying to locate the value, it is necessary to keep in mind that Parser is a case-sensitive language.
Example:
$stuff[^table::create{name pos Alexander Boss Joseph engineer Ralph art-director }] ^if(^stuff.locate[name;Ralph]){ The entry is found in line ^stuff.line[].
$stuff.name: $stuff.pos
}{ No such entry }
The code will output:
The entry is found in line 3. Ralph: art-director
^table_name.select(selection_criterion)
The method looks through the table row by row, examining each row in respect to the specified criterion (a mathematical expression). The rows which satisfy the criterion (returned Boolean value is "true") are collected into the table with the same structure as that of the initial table.
Example:
$men[^table::create{name age Serge 26 Alex 20 Mishka 29 }] $thoseAbove20[^men.select($men.age>20)]
Variable $thoseAbove20
will contain table made up of the rows with Alex
and Mishka
.
^table_name.hash[column_serving_as_key] ^table_name.hash[column_serving_as_key][column_of_values] ^table_name.hash[column_serving_as_key][table_with_columns_of_values]
The method transforms a table into hash with the following structure:
$hash[ $.value_from_column-key[ $.column_name[value] ... ] ... ]
In other words, the method creates hash, where the values from the specified column serve as hash keys. Here, every key has a relevant hash, where the keys are the names of all table's columns.
If a column of values is specified, every key will have a relevant hash with one key/value pair (the name of the specified column).
Besides, one may specify several columns to serve as keys of hash relevant to the specified column. To this end, as an additional parameter a table must be given with all necessary columns listed. The table must be specified as a substitute for the field of values.
This method may relate one table to another according to a key column.
Example:
We have a list of goods, where each item has a name and a unique code (id). We also have a price-list of available goods. Instead of the name of each item, we use relevant ids given in the goods list. This all is stored in two tables, which we refer to as "linked". We need to get data in the format "item-price"that is, to get data from two tables simultaneously.
Realisation:
# this is the table with goods $product_list[^table::create{id name 1 bread 2 meat 3 butter 4 whisky }] # this is the table with prices $price_list[^table::create{id price 1 6.50 2 70.00 3 60.85 }] #hash of the table with prices by id field $price_list_hash[^price_list.hash[id]] #looking through the entries of the table with goods ^product_list.menu{ $product_price[$price_list_hash.[$product_list.id].price] #checking if there is a price for the item in our hash ^if($product_price){ #outputting item's name and price $product_list.name - $product_price<br> }{ #and this item has no price, i.e. is unavailable $product_list.name - unavailable<br> } }
The output will be:
bread - 6.50 meat - 70.00 butter - 60.85 whisky - unavailable
^table_name.columns[]
The method creates named table consisting of sole column (called column
) containing column names of the initial table.
Example:
$columns_table[^stuff.columns[]]