LabVIEW Idea Exchange

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

Implement Python-Style Array Indexing

I think it would save a bit of space, and time, if we could use a negative array index value in order to quickly get the last element of an array.  This would also work for 2nd to last item, 3rd to last, etc.

Python Array Index Example

When I started working with Python I thought it was rather useful and intuitive to be able to quickly reference the last item in an array in that way.  Like so:

 

>>> a= [1, 2, 3]
>>> print a[-3]
1
>>> print a[-2]
2
>>> print a[-1]
3

10 Comments
garrettmarsh
Member
ToeCutter
Active Participant

Like the idea, but want to keep array indexing quick and light and this would add another branch in the internal code. As an alternative, how about NI adds a separate function 'Index array from end'?

crossrulz
Knight of NI

Use Reverse 1D Array before indexing if you want to go from the end.


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
garrettmarsh
Member

Also, if this were implemented, you would run into the problem of breaking code which relies on the current behavior (that a negative index returns the default value for that data type).

GregFreeman
Trusted Enthusiast

I agree with garrett...I think this is not feasible for the reason he listed. 

lvABC
Member

I use Delete From Array Function to get last element where the default is the index of the last element in the array.
http://zone.ni.com/reference/en-xx/help/371361j-01/glang/delete_from_array/

 

last.png

nodnarb90
Member

Getting the array size and decrementing isn't so cumbersome that I think another method should be added specifically for that function, but I do realize it would probably increase the operations it takes for that function to complete.  I would just like to have the ability to index in the way that Python does.

 

I am aware that an "out-of-bounds" index will return the default value for that array data type, whether that index is too large or too small (negative), however, I don't believe I personally have ever created code that relied upon that.

 

The idea of a negative index is universal and could also be applied to other functions, such as string subset, to quickly get the extension.  I do realize there are functions to do this, and that it wouldn't be done this way because not all extensions use 3 characters, but this is just a demonstration for demonstrations sake.  See below.

 

example.png

 

Thanks for all the input!

altenbach
Knight of NI

I think we should keep all discussions in the original idea thread to avoid duplication of effort. I don't want to repeat everything I alread said.

JordanG
NI Employee (retired)
Yogesh_Redemptor
Member

To my knowledge this may lead to confusion, because '-1' index mean not valid or search not found.

We should leave '-1' for the same and should not use for other purpose like to index last data.

 

Regards,

Yogesh R

Regards,
Yogesh Redemptor