Negative Indexes in Nth(), Slice(), Splice(), Left()

In many programming languages (eg: JavaScript, Python, Java, C#, Clojure, etc.) you can use a negative index to select items in arrays or characters in text counting from the end of the array or string.

Positive indexes count from the start. Negative indexes count from the end.

So I tried this out in Coda and it works! At least for the Nth(), Slice(), Splice() and Left() functions.
Even though this feature is not included in the documentation.

It does not work for Right() or Middle() alas.

Max

23 Likes

I’ve also noticed that you can use negative numbers in the rounding formulas. -1 would round to closest 10, -2 to closest 100, etc

Easter eggs?

7 Likes

Good to know! Hmm. Wouldn’t it be cool if the Coda documentation on formulas allowed us to add comments to them so they would know where updates were needed. Hmmm…

3 Likes

Right Peit! I also noticed it and use it all the time. I think I got the idea initially from Excel and it still works here in Coda

2 Likes

Great post!!!

One more formula to add where I use negatives, Workday(). If you know the end date, or launch date, you can count backwards to the day you need to start by adding a negative to the number of workdays required. You can multiply by -1 but I thought it was kind of cool that you could go back and just add the minus sign next to a variable.

2 Likes

The second example here is using a negative in the Slice() formula to split first and last names. You can get creative with this trick!

2 Likes

Hey @Xyzor_Max this is great - I can use -1 to get the second to last item, but if I want to splice on to an existing array, is there a shortcut to get the last item in the array? I was hoping 0 would work, but no. -1+1 doesn’t work either. I know I could do [array].count() but looking for an easter egg.

use 9999 to insert at the end of the list.

you are right. -1 inserts to the 2nd last position and not the last position.

so i use 9999. if the position you give greater than the size of the list, then it splices to the end of the list.

Splice(myList, 9999, 0, “xxxx”)

appends “xxxx” to the END of myList

does that count as an easter egg?
max

1 Like

Its like the yolk of the egg - the good part, but not quite the whole thing :wink:

Thank you