Forums / General / How to assign new values to keys in hash?

How to assign new values to keys in hash?

Author Message

Aaron Weiss

Wednesday 24 March 2004 3:57:06 pm

I'm just getting the hang of the EZ template language. I'm familiar with coding in Perl/PHP, etc. Basic question: I have created a hash like this:

{let myhash=hash('key1','val1')}

How can I change the value for key1? I've tried all kinds of variations like:
$myhash[key1]="val2"

But this doesn't work. I tried some of the array functions like merge but that didn't work either. Ideas?

thanks!
Aaron

Bruce Morrison

Thursday 25 March 2004 12:37:13 am

Hi Aaron

I think I looked into this a couple of versions ago and didn't find a solution. Have you tried using the set operator ?

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Dan C

Wednesday 16 February 2005 1:11:09 pm

{set myhash=hash('key1','val1')}

In case anyone else has this question, I've found the following to work:

{set myhash=$myhash|merge( hash( 'key1','newVal' ) )}

Dan

Patrick ALLAERT

Wednesday 27 July 2005 9:02:23 am

Remark: You cannot use numeric keys in a hash !

In case you have to create a hash based on numeric keys within a loop, here is my code:

{section loop=... var=result}
  {set list=$list|merge( hash(
    '_'|append($result.item.contentobject_id),
    'some values...' ) ) }
{/section}

This is useful when doing computation !

Patrick ALLAERT
http://www.dixite.com/
http://users.pandora.be/patrick_allaert/