JJUPIN/SIL - How to remove empty/null elements from an array?

Hi there.

I'm struggling to remove null/empty elements from a string array. I can't find a way to remove such elements without sorting or converting to unique set. I will appreciate any hints how can I do the above operation.

Example code:

string source_string = "testelem1,,testelem2,,,testelem3,testelem2";

string[] my_array = split(source_string, ",");

I have tried :

  •     deleteElement(my_array, "")
  •     deleteElement(my_array, "null")
  •     deleteElement(my_array, null)

None of the above worked.

 

So far the I have found only two possibilities, both messing up order of elements in original array thus not fitting my needs:

  1. sort array, loop check 0th element for isNull() and remove it with deleteElementAt if true
  2. convert to set of unique elements by arrayToSet, check 0th element if isNull() and remove it with deleteElementAt if true.

2 answers

1 accepted

Hi Blazej,

deleteElement actually works, but stops on first occurrence, so you'll need something like this:

number sz;
do {
    sz = size(arr);
    arr = deleteElement(arr, "");
} while(sz != size(arr));

Another option would be to use arrayDiff

arr = arrayDiff(arr, {""});

Note: your "my_array" is declared as string not array.

Hope this helps!

Of course, I ate [] here :) arr = arrayDiff(arr, {""}); works perfectly! That's exactly what I've been looking for, thanks!

Of course I could just loop through the whole original array and copy all not null elements to another array, but it seems a bit overkill with large arrays :)

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

240 views 0 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot