SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default array index (0 or 1)?

    I'm working on an application that requires me to create a coordinate system and after doing some planning I realized a general pattern that emerged was this n+1 to handle the offset of my array being initialized as 0, which is standard for most languages. So is it acceptable to initialize an array's index at 1? I'm not using Lua for this project, but I'm aware that it's default index is 1 for table/array.

    I'd include the link to the wiki entry for lua, but sitepoint won't let me since I'm a noob poster. Feel free to wiki it and look for the #Table_as_array section. Or append that ID anchor to the link.

    Curious what people's experiences have been utilizing this method. I've only encountered this one other time, someone had offset the array to 1 and as a result it made refactor their code difficult, wish I could recall the exact details, but I do remember thinking it was odd they initialized the array's index to 1. So wasn't sure if there are rules or criteria for when it is acceptable to offset your array index.

    thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,269
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Hi nycCoder, welcome to the forums,
    I have no experience with Lua but using a "1 based" array index seems weird to me, especially since
    Lua arrays are 1-based: the first index is 1 rather than 0 as it is for many programming languages (though an explicit index of 0 is allowed).
    they know many programming languages use 0.
    I'd like to know if the logic behind the decision is based on actual need or rather a personal idea about how things "should be".
    Perhaps a newbie finds 0 counter-intuitive, but anyone who's done programming in any of the "many" languages should find it acceptable.
    As for refactoring, maybe you can initialize with -1 so the +1 will bring it to 0?

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I know, the rationale behind Lua's design, is that it's more intuitive with a base of 1. This is partially based on the target audience (people with little programming experience). However, most programming languages use zero as the base, so that's pretty much the de-facto standard. I would use whatever is the language default.

    Lua is a very function-oriented language btw., so you would rarely need an explicit array-index in properly written code.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This reminds me of XPath, where if you're trying to access the children of an element, the index starts with 1 rather than 0:
    Code:
    '//div/p[1]'       - first paragraph in a DIV
    It's somewhat similar to JavaScript's childNodes (which starts at 0) and it seems a bit counter-intuitive for it to start at 1 in XPath, as it just creates confusion.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    This reminds me of XPath, where if you're trying to access the children of an element, the index starts with 1 rather than 0:
    Correct me if I'm wrong, but I seem to recall that this changed in some version of XPath, so that if you use a newer version, if has zero as offset. (Which further adds to the confusion)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •