Quick and simple question - is Negative array indexes possible?
~Popper
Quick and simple question - is Negative array indexes possible?
~Popper
no
No it’s not.
Try an offset or absolute value.
offset,care to expain?
~Popper
You should read a bit of C/C++ to understand what arrays are and how they function.
http://www.cplusplus.com/doc/tutorial/arrays/
i know how they work, been programming for 14 yrs, just not have to use negative index arrays before.
Then you understand that a negative index (offset) will result in a value outside of your array. That might be a trick acceptable to do in C/C++ where you can access this way the value of a structure you know, but otherwise it will return complete garbage.
Actually negative indices can be used for counting from the end of the array (depending on the language). However that’s not possible in Unity, although you can of course just do array.Length-1 and so on.
–Eric
Just for my curiosity: what languages? I only found an example of ruby behaving that way.
I was thinking of Ruby actually, though I thought I’ve seen that elsewhere as well.
–Eric
What i meant was just a simple offset…if the lowest index is -10 you offset it by 10 so in the end its 0, -9 is 1 etc…
Python allows it. Boo too. Technically C would allow it with pointer funkery, though that’d be a terrible idea usually. Which means ObjC and C++ as well.
It’s just a syntactic sugar, really. Any language could do it.
It’s just “replace array[-x] with array[length of array - x]” in the compiler/interpreter.
You could easily write a generic container class with this feature in Mono.
Aside from maybe wrapping around the end of an array, what use cases would negative indices be useful for?