Question

Get the last Index of an Array?

  • 25 April 2018
  • 7 replies
  • 69 views

How can we use AttributeCreator which receives an array of data to only pull the last index of that array?


7 replies

Userlevel 5

Can you give us an example of what the array looks like?

Userlevel 2
Badge +16

If you are talking about lists in FME: The ListElementCounter will count the number of elements in a specified list. The highest index is the result of the ListElementCounter -1 (lists start at 0).

Userlevel 5

If it's an attribute list, you can use the following functions in an AttributeCreator.

To get the last index of _list{}:

@Evaluate(@NumElements(_list{})-1)

To get the last value of _list{}:

@Value(_list{@Evaluate(@NumElements(_list{})-1)})
Userlevel 5

Finally, you can use a ListIndexer with index set to -1 (=last element).

Here's an example of what the array looks like:

capture.png

200 results contain 213 attributes of this array. I would like to get the last index of each of those arrays/nested arrays. For example, if d:ArrayOfanyType{1}.anyType{11} is the last array for d:ArrayOfanyType{1}, I want to get its value.

I hope this is clear, let me know if you need more clarification.

Badge +7

If it's an attribute list, you can use the following functions in an AttributeCreator.

To get the last index of _list{}:

@Evaluate(@NumElements(_list{})-1)

To get the last value of _list{}:

@Value(_list{@Evaluate(@NumElements(_list{})-1)})
NumElements is the key - thanks for posting that answer.  I was getting frustrated that FME refused to accept }.  I still think it should be allowed, and/or you should be able to specify _list{-1} to get the last element in the list as per ListIndexer.

 

Badge +7

Posted an idea to allow use of negative numbers to get list elements from the end of list e.g. _list{-3}:

https://knowledge.safe.com/idea/72944/use-negative-numbers-to-get-list-element-from-end.html

Reply