Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The first item can be at any index - it just has to be the first one.

Some like 0. Some prefer 1. I think my suggestion of 7, more easily reached by the right index finger from the home row, has merit.



"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration." - Stan Kelly-Bootle


That sounds familiar, now you mention it. Fascinating. I wonder what percentage of my thoughts are actually original.


I tend to think that for a "this is a useful way to use them", that pascal did it right.

    Type
      LatLonTemp = array[-180..180] of array[-90..90] of Real
Arrays may start and end at any value of an ordinal type.

There are other problems with arrays in pascal (hinted at the type definition), but that the first element of the array doesn't have to be 0 or 1 or even a positive number can make some code easier to work with.


Can I index by 100s so there is plenty of room between them to add more indexes later?


In Ada yes. It will work in Lua too but iterators aren't guaranteed to go in order if you do.


Ada indexes have to be continuous ranges. So you'd need a type that was only every 100th number in order to use them as indexes, which I don't think can be made (I've never seen it, and looking it up I'm not seeing any way to make it). But you can select any arbitrary range as your array index, very handy as it neatly resolves the 0-/1-based issue: Choose the natural range for your situation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: