hash. Transforming a table into hash with specified keys

^table.hash[key]
^table.hash[key][options]
^table.hash[key][column_of_values]
^table.hash[key][column_of_values][options]
^table.hash[key]{code that forms a value}
   [3.4.5]
^table.hash[key]{code that forms a value}[options]
   
[3.4.5]
^table.hash[key][table_with_columns_of_values]
^table.hash[key][table_with_column_of_values][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:
$hash[
   $.value_of_key[
      $.name_of_column[value_of_column]
      
   ]
   
]

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.
$.type[hash/string/table]
[3.2.2]
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.
$.distinct(true/false)
false=identical values in key column are considered error (default);
true=get identical values from key column.
$.distinct[tables]
[3.0.8]
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.


Example
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.

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){ 
#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:
    bread-6.50
    
meat-70.00
    
butter-60.85
    
whisky-unavailable



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