# Painless script arrays

**URL:** https://discuss.elastic.co/t/painless-script-arrays/276162
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting, painless
**Created:** [June 16, 2021, 3:51pm UTC](https://discuss.elastic.co/t/painless-script-arrays/276162 "2021-06-16T15:51:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nickgregz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickgregz/32/54533_2.png) [@nickgregz](https://discuss.elastic.co/u/nickgregz)
#### Post date: [June 16, 2021, 3:51pm UTC](https://discuss.elastic.co/t/painless-script-arrays/276162/1 "2021-06-16T15:51:40Z")

</div>

Hello,

Is there a way to easily create and empty string array in Painless script, than add a string to it if it doesn't already exist in the array. I can't seem to find proper examples of arrays for Painless script anywhere.  
Thanks!

---

<div class="post-metadata">

### Author: ![Iker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iker/32/91708_2.png) [@Iker](https://discuss.elastic.co/u/Iker)
#### Post date: [June 16, 2021, 6:46pm UTC](https://discuss.elastic.co/t/painless-script-arrays/276162/2 "2021-06-16T18:46:06Z")

</div>

Painless is mostly Java, so you can use the same logic and the majority of the methods, a basic example will be:

```auto
String[] myStrings = new String[4];
boolean exist = false;

myStrings[0] = "Hello";
myStrings[1] = "World";
myStrings[2] = "From";
myStrings[3] = "Java";

String textInput = "Hello";

for (int i = 0; i < myStrings.length; ++i) {
    if (textInput.equals(myStrings[i])) {
        exist = true;
    }
}

return exist;

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 14, 2021, 6:46pm UTC](https://discuss.elastic.co/t/painless-script-arrays/276162/3 "2021-07-14T18:46:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
