hash. Transforming a table into hash with specified keys

^table.hash[key]{code that forms a value}
^table.hash[key]{code that forms a value}[options]

Key may be specified as:

·[string]-name of column, whose value will be regarded as key;  
·{code}-code, whose result will be regarded as key;  
·(mathematical expression)-whose result will be regarded as key.  

With default options the method transforms table into hash of the following structure:

In other words, the method creates hash, where the values from the specified column serve as hash keys. Every key is associated with a hash, where the keys are the names of all table's columns.

If a column of values is specified, every key will be associated with a 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-in this case, as an additional parameter a table must be given with all necessary columns listed.

Options-hash with transformation options.
hash=each hash item contain hash (default);
string=each hash item contain string. You must specify one column_of_values;
table=each element containing table. Using this option you can't specify column_of_values or table_with_column_of_values. This made for save memory because of tables in resulting hash just have links to tables' rows which already exist in memory.
false=identical values in key column are considered error (default);
true=get identical values from key column.
make up hash of tables containing rows with key.
Deprecated option which do the same as $.distinct(1) and $.type[table] if they specified together.

We have a list of goods, where each item has a name and a unique 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 referred to as "linked". We need to get data in the format "item-price", that is to get data from two tables simultaneously.


# this is the table with goods
create{id   name
1   bread

# 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

#looking through the entries of the table with goods 
#checking if there is a price for the item in our hash
#printing 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:

Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 26.04.2017