# Reverse of a list of strings in painless

**URL:** <https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065>\
**Category:** Kibana\
**Created:** [May 25, 2017, 3:19am UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065 "2017-05-25T03:19:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xntang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xntang/32/5606_2.png) [@xntang](https://discuss.elastic.co/u/xntang)\
**Post date:** [May 25, 2017, 3:19am UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/1 "2017-05-25T03:19:45Z")

</div>

How to call the reverse API in painless for a list of strings?

Basically I would like to do:

split()  
reverse()  
...

However, painless always complains there is no dynamic method reverse for string[].

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [May 25, 2017, 3:59pm UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/2 "2017-05-25T15:59:02Z")

</div>

Hi

It should be available dynamically, but it would be available on a List not an array. I don't think we provide much of an API for arrays and unfortunately split for string right now requires regular expressions, so the you will have to have those enabled. The other thing is we are working on improvements to the documentation, and while this doesn't cover your specific issue yet, it does cover types and operators more thoroughly

[https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-specification.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-specification.html)

I will try again to see if there is any good way to solve this, but for now, this is what I have got.

Thanks  
Rashmi

---

<div class="post-metadata">

**Author:** ![xntang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xntang/32/5606_2.png) [@xntang](https://discuss.elastic.co/u/xntang)\
**Post date:** [May 26, 2017, 9:52am UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/3 "2017-05-26T09:52:13Z")

</div>

I finally made the java code working, and it requires significant type casting ☹

String[] aa;  
aa = domain.split("\.");  
List ss = Arrays.asList(aa);  
Collections.reverse(ss);

However， translating the above pseudo Jave code into painless is a nightmare. Painless is far less from a working language and it would be great if the working Java code could work right away within painless.

---

<div class="post-metadata">

**Author:** ![xntang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xntang/32/5606_2.png) [@xntang](https://discuss.elastic.co/u/xntang)\
**Post date:** [May 26, 2017, 9:56am UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/4 "2017-05-26T09:56:59Z")

</div>

By the way, how to write painless code across multiple lines?

Should I use:

Can I write:

if (x \> 0) {  
...  
}  
across three lines?

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [May 26, 2017, 4:56pm UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/5 "2017-05-26T16:56:49Z")

</div>

Hi

Can you please let us know which part of that code you wrote doesn't work in painless and provide errors. Also you can surely write it across multiple lines; however if you are using the JSON to make requests, you must make script JSON compatible (i don't think JSON allows for multiple lines in this way)

Thanks  
Rashmi

---

<div class="post-metadata">

**Author:** ![xntang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xntang/32/5606_2.png) [@xntang](https://discuss.elastic.co/u/xntang)\
**Post date:** [May 27, 2017, 12:17am UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/6 "2017-05-27T00:17:55Z")

</div>

> [@xntang](#):
>
> aa = domain.split(".");

doesn't work in painless

---

<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:** [June 24, 2017, 12:17am UTC](https://discuss.elastic.co/t/reverse-of-a-list-of-strings-in-painless/87065/7 "2017-06-24T00:17:56Z")

</div>

Closed on June 24, 2017, 12:17am UTC

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