eZ Community » Forums » General » How to assign new values to keys in...
expandshrink

How to assign new values to keys in hash?

How to assign new values to keys in hash?

Thursday 25 March 2004 12:57:06 am - 4 replies

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

Thursday 25 March 2004 9: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

Wednesday 16 February 2005 10: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

Wednesday 27 July 2005 6:02:23 pm

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 !

Modified on Wednesday 27 July 2005 6:03:20 pm by Patrick ALLAERT

Wednesday 14 December 2011 2:50:48 pm

@Dan C.

If the keys are numeric, that method will not work.

@Patrick ALLAERT Re: "You cannot use numeric keys in a hash!"

The documentation provides an example using numeric keys in a hash.(http://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-operators/Arrays/hash)

I would like to see the documentation updated with non-numeric examples, especially how to build a hash with a loop.

For my example, I need a hash of the user bookmarks:

 {set $bookmarks_hash = $bookmarks_hash|merge( hash( $bookmark.id, $bookmark.node_id) )}

But this does not preserve the hash index. They are merged into array indices [0, 1, 2, ...].

If I add an underscore and make the hash index into a string, then it works!

 {set $bookmark_hash = $bookmark_hash|merge( hash( concat('_', $bookmark.id), $bookmark.node_id) )}
expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from