3 ms·
> To mark a node at the third position, containing one character, we say {3,3}. > But what if there's a node at the third position, that contains no character?
by saagarjha 6y ago
> To mark a node at the third position, containing one character, we say {3,3}.
> But what if there's a node at the third position, that contains no character? Well, thats {3,2}
Why not shift over indexing by one consistently? So you'd use {3, 4} and {3, 3} for each, so the length property is preserved.
- samatman 6y agoBecause that's not how the string library works. ("12345"):sub(3,3) returns "3", while ("12345"):sub(3,4) returns "34", and ("12345"):sub(3,2) returns "". Indexing in the array portion of tables, and strings, is at least consistent; but Djikstra was right. The Wiki[0] has a good discussion of Djikstra's iconic essay. The best of these arguments, imho: 0 based indexing unifies enumeration and measurement. [0]: https://wiki.c2.com/?WhyNumberingShouldStartAtZero https://wiki.c2.com/?WhyNumberingShouldStartAtZero