hash
Summary
Creates and returns a new associative array (a hash).
Usage
hash( key1, value1 [, key2, value2 [, ... ] ] )
Parameters
| Name | Type | Description | Required |
|---|---|---|---|
| key1 | string | The key of value1. | Yes. |
| value1 | any | The value associated with key1. | Yes. |
| key2 | string | The key of value2. | No. |
| value2 | any | The value associated with key2. | No. |
Returns
An associative array (a hash).
Description
This operator builds an associative array using the specified key/value pairs. Odd parameters are considered to be keys, even parameters will be values. The operator returns the generated hash.
Examples
Example 1
{hash( 1, 'Red Eyes', 2, 'Green Gremlins', 3, 'Blue Thunder' )}The following hash will be returned:
| Key | Value |
|---|---|
| 1 | Red Eyes |
| 2 | Green Gremlins |
| 3 | Blue Thunder |
Contributors
- Balazs Halasy Updated
- Santiago Commenter
- Vittal Aithal Commenter
- alfred Commenter
Comments (4 historical)
Written by readers of the earlier documentation sites, kept as they were.
Adding elements to exiting hash
Santiago ·
Is it possible to add new elements (a new pair key/value) to an existing hash?
Re: Adding elements to exiting hash
Vittal Aithal ·
Yes, it is possible to add items to an existing hash (though the syntax is crabbed). The following example shows how items can be added:
{def $items=hash('bar','boff')}{set $items=$items|merge(hash('foo','some text','ping','poot'))}foo = {$items['foo']}<br/>ping = {$items['ping']}<br/>bar = {$items['bar']}The example initialises a hash, and then adds two extra pairs to it using the merge operator.
Re: Re: Adding elements to exiting hash
alfred ·
and, how to replace a value?
{def $items = hash('aaa', 'A')}
{$items.aaa='B'}
it gives me error.
Re: Re: Re: Adding elements to exiting hash
alfred ·
nor {set $items['aaa']='BBB'}