I just noticed, by answering a question on StackOverflow, that the documentation on "arrays" could be enriched. Notably on the different ways to extract one or more elements.
But before I do that, I want to make sure I don't forget anything.
Let's take an example of an array: the list of column names in a project.
row.columnNames
[ "ID", "TYPE", "TXT_FRE", "x", "y", "Code postal", "Commune" ]
If you want to select all the elements except the first one, you can use slice(), true?
row.columnNames.slice(1)
[ "TYPE", "TXT_FRE", "x", "y", "Code postal", "Commune" ]
To select the first 3 elements, it would be:
row.columnNames.slice(0,3)
[ "ID", "TYPE", "TXT_FRE" ]
Brackets can also be used to isolate one or more elements, for example all except the last:
row.columnNames [0, -1]
[ "ID", "TYPE", "TXT_FRE", "x", "y", "Code postal" ]
If we want to select an element by its value, we could use filter():
filter(row.columnNames, v, v == "ID")
[ "ID" ]
But how to select multiple elements of an array by their values? So far, I have found no alternative to using Python/Jython:
return [x for x in row['columnNames'] if x in ["ID", "TYPE", "Code postal"]]
[ "ID", "TYPE", "Code postal" ]
Or is there another method to get the same result in GREL that I have forgotten?