Expression: get 3rd to last element of split(..) result

I've never been real happy with how expressions deal with common sub-expressions. That is, not happy at all. The impetus to find a solution isn't there in Perspective, because you can put such expressions ahead of a transform so they will only be executed once. (And if you have more than one, my new asList() expression can bundle multiple such into a single value.)

But expression tags and Vision UI bindings do not have transforms. :frowning_face:

I realized while reading this topic that I've already created a combination that can do this, and using len() on the result of split() is the perfect test case:

forEach(
	asList(
		split({path.to.some.string}, '/')
	),
	it()[len(it())-1,0]
)[0]

The split() in the center of this expression is executed once, the result dataset becomes the one element in a list to be iterated, and therefore handed to both calls to it() within the loop (which runs just the once) to build the output list. Take the one element of that output list as the final output.

Yielding the last element of the path, but only referencing the string property once, and only running split() once.

:grin:

1 Like