Zero-based list index
-
Lately I've been writing a script to automate some tasks in InDesign, with a little window to set some options. This includes a drop-down list with stuff in it, from which you can find out the user's selection by the value of the list's selected index property. This index is zero-based, [i]except for the last item[/i] — the index of that equals the number of items in the list.
-
So, a five item array would be:
lwddb[0] =first value
lwddb[1]=second value
lwddb[2]=third value
lwddb[3]=fourth value
lwddb[4]=crash
lwddb[5]=fifth value.
Hmm. Me thinks some marbles have been lost. I cannot rightly comprehend the confusion of ideas that would lead to such a construct, nor the glorbishly fromulent code required to handle it! for(i;i<len -1;i++) {code}; i=len; copy-paste of code, perhaps? or lots of {i==len-1?i+1:i}'s? The mind boggles.
-
Somewhere, someone got angry writing stuff like
list[list.size - 1]
.This is even stupider than zero indexing months, because at least that's consistent.
-
Perhaps the problem could be mitigated a bit with something like: for (i = 0; i <= length; i++) if (i != length - 1) { code; }
Though really, this should never have been an issue to begin with.
I suggest tracking down the person responsible and seeing how many fire pokers can be inserted into them.
-
You're sure the second-last item isn't a separator you forgot to count?
-
foreach(Row in Tables.Rows)
{
lists.add(Row["value"])}
lists[lists.length [b] + 1[/b]] = "I am the last value standing ahahahahahahahahah!"
Or something like that
-
@robbak said:
So, a five item array would be:
lwddb[0] =first value
[snip]Yep, as far as I can tell by some experimentation (logging the selected index and the contents of the array that I use to fill the list), that's exactly what happens.
@Salamander said:
Perhaps the problem could be mitigated a bit with something like: for (i = 0; i <= length; i++) if (i != length - 1) { code; }
Though really, this should never have been an issue to begin with.That is, indeed, pretty much the solution I came up with as well, and also my conclusion. I still wonder if there's something I'm missing, but not as far as I've been able to tell.
@flabdablet said:
You're sure the second-last item isn't a separator you forgot to count?
I've got an array of strings that I use to create list items from; I get the same number of list items as I have elements in the array, and in the same order. Having the script log the selected index number gets me 0 if I select the first item, 1 for the second, 2 for the third, and so on as you would expect, but not for the last item, which returns the number of list items. I added extra items to the array/list and removed them from it, and the exact same thing happens no matter how many items there are …
The problem wouldn't exist if I could get the displayed name of the item that the user selected from the control, but it apparently only returns the index and an array of strings containing [i]all[/i] the list items (as far as things useful to this problem go, anyway). But since I already have an array with those, there's no point in trying to get something useful from that either, I'd say.