Skip to main content
Open

Use negative numbers to get list element from end of list

Related products:Transformers
  • Jun 22, 2018 12:00 AM UTC
  • 5 replies
  • 180 views

tim_wood
Contributor
Forum|alt.badge.img+8

See https://knowledge.safe.com/questions/68957/get-the-last-index-of-an-array.html

I now know how to access the last element in a list in a single statement thanks to @david_r's answer to this question. So I no longer have to count the list elements then subtract 1 from it before getting the value of the last element in the list. However, in the process of finding out how to do this, I discovered that the ListIndexer allows you to specify a negative number to start from the end of the list.

I haven't yet had a scenario where I needed to get (for example) the 3rd element from the end of the list but these situations are bound to arise at some point! So I'd like to suggest that negative numbers be allowed e.g. _list{-3} to start from the end of the list rather than the start.

5 replies

tim_wood
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • Nov 22, 2019 6:28 PM UTC

In addition to the answer in the question linked to above, you can also do it this way if you have used ListElementCounter earlier in the Workspace:

@Value(_list{@Evaluate(@Value(_element_count)-3)})

The key thing is using @Evaluate() around the numeric bit.


LizAtSafe
Safer
Forum|alt.badge.img+26
  • Safer
  • Jun 9, 2026 8:15 PM UTC
Open→Gathering Interest

LizAtSafe
Safer
Forum|alt.badge.img+26
  • Safer
  • Jun 9, 2026 8:15 PM UTC
Gathering Interest→Open

LizAtSafe
Safer
Forum|alt.badge.img+26
  • Safer
  • Jun 9, 2026 10:34 PM UTC
Open→Gathering Interest

LizAtSafe
Safer
Forum|alt.badge.img+26
  • Safer
  • Aug 19, 2026 4:46 PM UTC
Gathering Interest→Open