With GREL is it possible to get the longest string of an array ? For example, if I have an array with 3 strings ["a","aaa","aa"], I want to obtain "aaa".
Asked
Active
Viewed 252 times
1 Answers
0
You can probably do that at the cost of a very complicated formula. It's typically to face this kind of case that Open Refine added Python (and Clojure) as scripting languages. Even if you don't know Python, you can find in two minutes the answer to the question "how to choose the longest string in list?" and simply copy and paste it (by adding a "return" instead of "print")
In this case :
return max(['a','aaa','aaaa','aa'], key=len)
EDIT
Just for the sake of the challenge, here is a possible solution with GREL.
value = "a,aa,aaaa,aa"
forEach(value.split(','), e, if(length(e)==sort(forEach(value.split(','), e, e.length()))[-1], e, null)).join(',').split(',')

Ettore Rizza
- 2,800
- 2
- 11
- 23
-
1Thanks. Good example of the usefulness of Python in some use cases. – Mathieu Saby Aug 11 '18 at 14:12
-
1`with(value.split(','), a, filter(a, v, v.length() == forEach(a, x, x.length()).sort()[-1]))[0]` – kev Dec 19 '18 at 08:52