# Looping through an array with accounts, but due to suffixes other arrays do not work

**URL:** <https://community.silverfin.com/t/looping-through-an-array-with-accounts-but-due-to-suffixes-other-arrays-do-not-work/2025>\
**Category:** Templates\
**Created:** [February 18, 2021, 12:58pm UTC](https://community.silverfin.com/t/looping-through-an-array-with-accounts-but-due-to-suffixes-other-arrays-do-not-work/2025 "2021-02-18T12:58:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahaegglund](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@ahaegglund](https://community.silverfin.com/u/ahaegglund)\
**Post date:** [February 18, 2021, 12:58pm UTC](https://community.silverfin.com/t/looping-through-an-array-with-accounts-but-due-to-suffixes-other-arrays-do-not-work/2025/1 "2021-02-18T12:58:33Z")

</div>

Hi,

I have several arrays (see below). In “account\_array” I can have 1-8 accounts per key, separated by “;”. And in the “default\_array” it lets the loop know if we should have “default:account.value” in our input, or if we should have “placeholder:0”.  
The problem I have encountered is that if for example for key\_001 and account 598000 have 598000.001, 598000.002 and 598000.003 etc then the loop does not understand that all of the 598000-accounts should have “default\_array” value = “no”. It loops through the default\_array and picks the value “no” for 598000.001, the value “no” for 598000.002 and the value “yes” for 598000.003.

How can I make the code understand that “no” should be applied by all the 598000-accounts? Below is part of the code, it is where the codes says “{% assign get\_default = default\_sub\_array[forloop.index0] %}“ where I think things go wrong.

{% capture key\_array %}key\_001;key\_002;key\_003;key\_004;key\_005;key\_006;key\_007;key\_008{% endcapture %}

{% capture account\_array %}598000,599000,699300;607000,607100,607200,763100,763200;634000,634100,634200;642200,642300,655000,658000;698000,698100,698200;762200,762300;842300;509900,519900,690000,699200,769000,850000,860000,900000{% endcapture %}

{% capture default\_array %}no,no,yes;no,no,yes,no,yes;no,no,yes;no,no,no,no;no,no,no;no,yes;yes;yes,yes,yes,yes,no,no,no,no{% endcapture %}

Part of the code:  
{% for item in key\_array %}  
{% assign description\_string = text\_array[forloop.index0] %}  
{% assign account\_col = account\_array[forloop.index0] %}  
{% assign tot\_accounts = period.accounts.include\_zeros | range:account\_array[forloop.index0] %}

```
| **{% t description_string %}** 

{% assign accounts_count = tot_accounts.count | integer %}
{% capture account_info %}{{ accounts_count }} {% t "konton" %}{% endcapture %}
| 
|
{% newline %}
{% assign default_sub_array = default_array[forloop.index0] %}
{% assign default_sub_array = default_sub_array | split:',' %}
{% capture result_name %}result_{{ description_string }}{% endcapture %}
{% for account in tot_accounts %}
  {% assign account_str = account.id %}
  | &emsp; &emsp; {{ account.number }} {{ account.name }}
  | {{ account.value | currency:0 }}
    {% assign get_default = default_sub_array[forloop.index0] %}
   {% if get_default == 'yes' %}
    | {%=$2+input custom.dis_allowed.[account_str] as:currency precision:0 default:account.value assign:input_amount %}
  {% else %}
    | {%=$2+input custom.dis_allowed.[account_str] as:currency precision:0 placeholder:0 assign:input_amount %}
  {% endif %}
  {% assign sub_total_amount = sub_total_amount+input_amount %}
  {% rollforward nil custom.dis_allowed.[account_str] %}
  {% newline %}
{% endfor %}

```

Kind regards,  
Anna

---

<div class="post-metadata">

**Author:** ![Melle](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/melle/32/1312_2.png) [@Melle](https://community.silverfin.com/u/Melle)\
**Post date:** [February 18, 2021, 2:46pm UTC](https://community.silverfin.com/t/looping-through-an-array-with-accounts-but-due-to-suffixes-other-arrays-do-not-work/2025/2 "2021-02-18T14:46:51Z")

</div>

Hi @ahaegglund,

Before you can use the arrays, you need to split these and I currently don’t see the code you are using to do that. Could you please provide the full code? Otherwise try

```
{% assign key_array = key_array | split:";" %}
{% assign account_array = account_array | split:";" %}
{% assign default_array = default_array | split:";" %}

```

…which should be applied after defining but _before_ iterating over the arrays.

Also keep in mind that `forloop.index0` always follows the closest (inner) loop you are iterating over. `default_sub_array[forloop.index0]` therefore is defined as many times as there are accounts in tot\_accounts (which can be many), while the `default_sub_array` has a couple of items in it at most (i.e. as much as there are yes/no statements under the current key). In your case, this may lead to a problem.

Does this help? Otherwise don’t hesitate to ask again.
