Answers.Body: <p>There's not a pretty way to do it in Twig other than what you've done in the <code>if</code> part of your conditional.  Something like this should work:</p>
  
  <pre><code>{% if attribute(data, entry_name) is not defined and entry_total &gt; 0 %}
      {% set data = data|merge({ (entry_name) : entry_total }) %}
  {% else %}
      {% set data = data|merge({ (entry_name): 10 }) %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-03T21:18:05.663Z
Answers.LastActivityDate: 2020-01-03T21:18:05.663Z
Answers.UserDisplayName: Brad Bell
Body: <p>Here is the code:</p>
  
  <pre><code>{% if entry_name not in data and entry_total &gt; 0 %}
      {% set data = data|merge({ (entry_name) : entry_total }) %}
  {% else %}
      {% set data.entry_name = 10 %}
  {% endif %}
  </code></pre>
  
  <p>Twig throws an error on the statement following <code>else</code> condition.  <code>Unexpected token "punctuation" of value "." ("end of statement block" expected).</code> How can I set the value of a particular element of this associative array?</p>
  
Tags: <templating><array>
Title: Error while accessing an associative array
ThreadText: # Error while accessing an associative array
  
  (Asked by: username_0 on 2020-01-02)
  
  Here is the code:
  
  ```
  {% if entry_name not in data and entry_total > 0 %}
      {% set data = data|merge({ (entry_name) : entry_total }) %}
  {% else %}
      {% set data.entry_name = 10 %}
  {% endif %}
  
  ```
  
  Twig throws an error on the statement following `else` condition. `Unexpected token "punctuation" of value "." ("end of statement block" expected).` How can I set the value of a particular element of this associative array?
  
  **Tags:** templating, array
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-03. Score: 1 | Accepted answer
  
  There's not a pretty way to do it in Twig other than what you've done in the `if` part of your conditional. Something like this should work:
  
  ```
  {% if attribute(data, entry_name) is not defined and entry_total > 0 %}
      {% set data = data|merge({ (entry_name) : entry_total }) %}
  {% else %}
      {% set data = data|merge({ (entry_name): 10 }) %}
  {% endif %}
  
  ```
  
  
  ---
  
  