Serial substitution - 2

Gentlemen, something that baffles me (although I’m probably overlooking something here):

{% assign str_All = 'Bart|Christophe|Cindy|Veerle|Sarah' %}

{% comment %}Step: hang laatste separator aan string{% endcomment %}
{% assign str_All2 = str_All | append:'|' %}

{% assign str_ToReplace = 'Cindy|Bart' %}
{% assign arr_ToReplace = str_ToReplace | split:'|' %}

{% comment %}Step: initialiseer het resultaat op de volledige string{% endcomment %}
{% assign str_Result = str_All2 %}

{% comment %}Step: doe serial substitution{% endcomment %}
{% for person in arr_ToReplace %}
  {% assign str_Temporary = person | append:'|' %} {% comment %}dit is om de opeenvolging van minstens 2 separators tegen te gaan{% endcomment %}
  {% assign str_Result = str_Result | replace: str_Temporary, '' %}
{% endfor %}

{% comment %}Als het laatste karakter een separator is, doe deze dan weg{% endcomment %}
{% assign str_Result_LastCharacter = str_Result | slice: -1, 1 %}
{% if str_Result_LastCharacter == '|' %}
{% assign str_Result = str_Result | slice: 0, str_Result.size-1 %}
{% endif %}

{% assign arr_Result = str_Result | split:'|' %}

str_All: {{ str_All }}
str_ToReplace: {{ str_ToReplace }}
str_Result: {{ str_Result }}
str_Result.size: {{ str_Result.size }}
arr_Result: {{ arr_Result.size }}

{% assign arr_Result_Fixed = 'Christophe|Veerle|Sarah' | split:'|' %}
arr_Result_Fixed: {{ arr_Result_Fixed.size }}

Up to arr_Result: everything goes well, but the split ({% assign arr_Result = str_Result | split:’|’ %}) does not work. With fixed values (arr_Result_Fixed), it works … so what am I overlooking here?


Hello @Bart_Verhaeghe,

Before we dive into your code (thanks for the contribution, as always!), could you explain on what the purpose is of this code?
Because on first glance, I might think you are trying to remove persons from something else (f.i. from all persons from the drop period.people ?). If so, then I might suggest something else. That’s why the context is also important :slightly_smiling_face:

Hi Sven,

Yes, that’s it … the goal is to get the difference between two arrays, one ‘all in’ and one ‘to remove’ so in the end, only the ‘result’ stays (that’s why I call it serial substitution).


Sure thing @Bart_Verhaeghe, but how are the names that need to be removed, generated then? They probably are not generated by letting users fill in the name, right?

Hi Sven, I found a solution (I replace the separators ‘|’ by ‘;’) and then, it works (but it’s a mystery to me why this works and the original solution didn’t).

A practical application: a letter is written by directors to other directors. As all the directors are listed, some of them are ‘chosen’ (with a checkbox) to sign the letter while the others (who are not indicated), are written a letter.

So If you’ve got 8 directors and 3 of them are indicated as ‘signing’, then the other 5 are adressed in 5 letters. That’s why the ‘anti-join’ has to be performed here …


Wouldn’t it be more user-friendly that create boolean (checks) for each director, and loop over the ones that have (or don’t have) a check? That way, you immediately have your needed string or array, no?

Perhaps I’m missing the point here though :blush: