3 ms·
Fun fact: arrays in Fortran are also based on 1 by default, but they can be defined to start at 0, or even arbitrary negative numbers. The following are all val
by wycy 8y ago
Fun fact: arrays in Fortran are also based on 1 by default, but they can be defined to start at 0, or even arbitrary negative numbers. The following are all valid:
integer :: array1(5)
integer :: array2(0:4)
integer :: array3(5:10)
integer :: array4(-4:4)
- dagw 8y agoJulia also supports this (called Offset Arrays)
- wycy 8y agoInteresting. Doesn't this solve the problem then for people who can't get into 1-indexed arrays?
- dagw 8y agoI suppose it kind of does for your own code, but you have to explicitly 'cast' all your arrays to 0-index arrays and it might break anything that assumes it's dealing with a normal 1-index array.
- dagw 8y agoAnd realistically in many cases you won't even notice the indexing as you'll either be iterating over the array or using an array operator rather than accessing elements via an index. And in the remaining cases, much of the time whether the index is 0-based or 1-based won't affect the way the final code looks. Still, it's nice to have the option to 0-index in the few cases where it does make your code cleaner.
- renox 8y agoThe problem is that it's not the default.
- wbl 8y agoPerl has a global variable that does the same thing.