Skip to the content
Documentation

Adding elements to exiting hash

No ratings yet

Is it possible to add new elements (a new pair key/value) to an existing hash?

Comments (3 historical)

Written by readers of the earlier documentation sites, kept as they were.

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

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

      1. Re: Re: Re: Adding elements to exiting hash

        alfred ·

        nor {set $items['aaa']='BBB'}