LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
ryz

Last element index

It would be preety nice if all the VIs that can be indexed in any way (for example string and array operations) would accept "n" and "n-1" and such as input to denote that the user want the last element/character/etc. or the element before the last and so on.

example.png

15 Comments
crossrulz
Knight of NI

I submitted a VI to OpenG to get the last element of an array.  You can find it here:http://lavag.org/topic/16226-new-array-vi/

 

I used the Reverse Array trick.  My benchmarks showed that it was faster than finding the array size, subtract 1, and index.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
ryz
Member
Member

You all wrote very useful solutions on how to index the last element of the second last etc. what I am missing is a generic solution or VI which could be applied for every data type which may be indexed in any way. Not just arrays but for example strings as well. You can use a workaround or a specific function for all types but I would welcome an universal constant that says that "I am refering to the last element (whatever it may be)".

And on a side note: I know it is already possible to address the last element and that there are a handful of ways to do it, but I believe that the purpose of an idea exchange should be to share an idea even if it is only a slight improvement on the current system. Smiley Wink

fabric
Active Participant

Kudos for the spirit of the idea. I agree that indexing from the end of an array is something so fundamental that it should be streamlined.

 

However, I don't really agree with the proposal for an "n-1" constant... Rather, I think the indexing mode should be a property of the node itself.

 

Intaris hinted at the key idea:

"I personally would like an option like setting the index of a file where we can select the "INDEX" input to the array element function and choose whether the index is measured from the start or end of the array."

 

That said, I could live with a slightly watered down version:

  • That an unwired index be interpreted as either the FIRST or LAST element, settable via a right-click option.
  • In line with this, the icon should change to reflect the current setting.

This would help to alleviate the existing confusion where some nodes (e.g. Index Array) default to the first element when unwired, while others (e.g. Delete From Array) default to the last. It would be nice if this applied to all relevant array primitives...

 

 

Another alternative (proposed here) is that negative indices count backwards from the end of the array, although that would also need to be a settable option so as not to break existing code...

MaryH
Member
Status changed to: Declined
Intaris
Proven Zealot

Is it declined AND a duplicate or declined BECAUSE it''s a duplicate?