Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. From where should the index start?

From where should the index start?

Scheduled Pinned Locked Moved The Lounge
questionphpdatabasedesignhelp
114 Posts 31 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nikunj_Bhatt

    In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    Your example is flawed: zero indexing can return "string not found inside another string" - and does - by returning a negative index. Why multiply methods if it is not necessary?

    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    1 Reply Last reply
    0
    • N Nikunj_Bhatt

      In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #3

      I'd start at zero. And have you tried Pascal?

      1 Reply Last reply
      0
      • N Nikunj_Bhatt

        In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #4

        Clearly, all indices should start from -2 and count in base 13.

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        OriginalGriffO A R C 4 Replies Last reply
        0
        • I Ian Shlasko

          Clearly, all indices should start from -2 and count in base 13.

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #5

          Did you have something to do with the development of APL?:~

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          B 1 Reply Last reply
          0
          • I Ian Shlasko

            Clearly, all indices should start from -2 and count in base 13.

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #6

            Clearly ;P

            1 Reply Last reply
            0
            • N Nikunj_Bhatt

              In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

              W Offline
              W Offline
              Wjousts
              wrote on last edited by
              #7

              nikunjbhatt84 wrote:

              the function can return Zero and thus making the IF condition false.

              No, because using zero as false is dangerous. That's why strongly typed languages (such as C#) have an actual bool class. So you don't accidentally mix up using an int as a bool when it was supposed to be an int.

              N L P 3 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                Did you have something to do with the development of APL?:~

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #8

                Now be fair, you can't set the index origin to -2 in APL. I think you're after VB or Pascal/Delphi here (both allow you to index arrays arbitrarily).

                OriginalGriffO 1 Reply Last reply
                0
                • N Nikunj_Bhatt

                  In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #9

                  Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address p and each item is an offset from there so, for item n where each element is size s the memory address is p+n*s. If you want 1 based arrays then the element would be found at p+(n-1)*s. Which is easier to compute?


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  N B W L P 7 Replies Last reply
                  0
                  • B BobJanova

                    Now be fair, you can't set the index origin to -2 in APL. I think you're after VB or Pascal/Delphi here (both allow you to index arrays arbitrarily).

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    I wouldn't be so sure, those characters are pretty impenetrable to mere mortals like me... :laugh:

                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    B 1 Reply Last reply
                    0
                    • N Nikunj_Bhatt

                      In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #11

                      Indices start from 1. (When did you ever hear someone talking about the 'zeroth' anything, outside of certain computing circles?) Offsets start from 0. The confusion comes because the C family really uses offsets (you can see this in the original C, with the identity a[i] = a + i), but they've always been called 'indices', and that has fed forward into '0 based indexing' (which means subscripts are offsets, really) in its modern siblings. Once you know whether you want to base your language on indices or offsets, the mechanics become obvious. Index: start from 1. ("What's the first/second/etc character of a string?" -> "s[1/2/etc]".) Offsets start from 0. ("What's the character 0/1/etc away from the start?" -> "s[0/1/etc]".) Since I come from the real world and not a computer science course I naturally think in terms of index origin 1 – particularly as I started out programming in APL with that setting. I've written conventional languages enough that I can deal with the offset-based approach but if I was choosing I'd still start at 1.

                      L 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        I wouldn't be so sure, those characters are pretty impenetrable to mere mortals like me... :laugh:

                        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #12

                        It's definitely true, I tried it one time, it's a domain error ;)

                        1 Reply Last reply
                        0
                        • N Nikunj_Bhatt

                          In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                          M Offline
                          M Offline
                          Marc Clifton
                          wrote on last edited by
                          #13

                          nikunjbhatt84 wrote:

                          Then from where would start the index number?

                          I wouldn't allow indexing. At least, I wouldn't expose it to the programming language. Indexing is an archaic left-over from the days for for-next loops and arrays. Even for strings, it's absurd. Folks should take a page from assembly language, where you could map memory into a structure and reference specific elements of the structure with meaningful symbols. This includes handling strings dynamically as well, if people were just to put there minds to it. Marc

                          My Blog

                          B L 2 Replies Last reply
                          0
                          • N Nagy Vilmos

                            Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address p and each item is an offset from there so, for item n where each element is size s the memory address is p+n*s. If you want 1 based arrays then the element would be found at p+(n-1)*s. Which is easier to compute?


                            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                            N Offline
                            N Offline
                            Nikunj_Bhatt
                            wrote on last edited by
                            #14

                            I think you are just considering pointers. Please think about overall problem. In modern programming (languages), importance of pointer is decreasing.

                            N 1 Reply Last reply
                            0
                            • M Marc Clifton

                              nikunjbhatt84 wrote:

                              Then from where would start the index number?

                              I wouldn't allow indexing. At least, I wouldn't expose it to the programming language. Indexing is an archaic left-over from the days for for-next loops and arrays. Even for strings, it's absurd. Folks should take a page from assembly language, where you could map memory into a structure and reference specific elements of the structure with meaningful symbols. This includes handling strings dynamically as well, if people were just to put there minds to it. Marc

                              My Blog

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #15

                              It's pretty difficult to work without lists, and you need to get things out of a list. Unless you count '1', '2' etc as 'meaningful symbols' in which case you still have indexing really. You can help a lot by having enumerations (foreach doesn't require indexing) and array-based functionality (so you can add two vectors without needing to loop around them), but a lot of real world problems would be much more difficult without being able to look things up by index in arrays or collections.

                              M P 2 Replies Last reply
                              0
                              • N Nikunj_Bhatt

                                In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                                D Offline
                                D Offline
                                Dalek Dave
                                wrote on last edited by
                                #16

                                Of course one should never start with an index, they should always be at the back, with the appendix.

                                ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

                                1 Reply Last reply
                                0
                                • N Nagy Vilmos

                                  Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address p and each item is an offset from there so, for item n where each element is size s the memory address is p+n*s. If you want 1 based arrays then the element would be found at p+(n-1)*s. Which is easier to compute?


                                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                  B Offline
                                  B Offline
                                  BobJanova
                                  wrote on last edited by
                                  #17

                                  Such low level considerations are why C family uses offsets, indeed. But that isn't particularly relevant for modern high level languages (even something as simple as an array isn't just a pointer in C# or Java, and most 'indexing' in C# or C++ is going through class code anyway). It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.

                                  N 1 Reply Last reply
                                  0
                                  • N Nikunj_Bhatt

                                    I think you are just considering pointers. Please think about overall problem. In modern programming (languages), importance of pointer is decreasing.

                                    N Offline
                                    N Offline
                                    Nagy Vilmos
                                    wrote on last edited by
                                    #18

                                    nikunjbhatt84 wrote:

                                    In modern programming (languages), importance of pointer is decreasing.

                                    What is an 'object'? Once you've got to the machine code, it is a lot of data held in memory location. How do you think the machine knows where to find that data? #ping# using pointers! Just because we don't call them pointers in the code does not mean they don't exist. Every object reference compiles down to a memory pointer. You are showing a huge level of ignorance if you really believe pointers are not important. A big mistake being made today is the lack of knowledge of the low level. By studying a bit of machine code you will see very quickly how the computer actually processes a program. Go study the Von Neumann architecture[^] to understand how a computer works. The less memory you use, the faster things can load. The less process steps you use, the faster things can run. Wow! Computing-101 and you don't have a fucking clue why it is important!


                                    Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                    N 1 Reply Last reply
                                    0
                                    • B BobJanova

                                      Such low level considerations are why C family uses offsets, indeed. But that isn't particularly relevant for modern high level languages (even something as simple as an array isn't just a pointer in C# or Java, and most 'indexing' in C# or C++ is going through class code anyway). It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.

                                      N Offline
                                      N Offline
                                      Nagy Vilmos
                                      wrote on last edited by
                                      #19

                                      BobJanova wrote:

                                      It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.

                                      How? See my comment above, by reducing the number of steps, you produce more efficient programs.


                                      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                      B P 2 Replies Last reply
                                      0
                                      • N Nikunj_Bhatt

                                        In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                                        G Offline
                                        G Offline
                                        gumi_r msn com
                                        wrote on last edited by
                                        #20

                                        If it were to be a clean start, I'd definitely have an index start with '1'. As to why, the reason is simple, it's they way we count since we're 2 years old. How does a small kid learn his numbers? I find it hard to imagine some little boy counting 0, 1, 2, ... We start at 1, and do so even when we are grown ups. Zero based indexing is just a legacy of array pointers and offsets. It doesn't help in any useful way in modern languages, it only makes things more confusing, its cumbersome to referrence the last item of an array and it doesn't agree with how we normally count things; when you go to a grocery store and you ask for 10 apples, you expect to get ten apples, them being apple nº1, apple nº2, ... , apple nº10, definitely not apple nº0, ..., apple nº9. So why when you "buy" a 10 item array you get the latter and not the former? WTF?!?

                                        L P 2 Replies Last reply
                                        0
                                        • W Wjousts

                                          nikunjbhatt84 wrote:

                                          the function can return Zero and thus making the IF condition false.

                                          No, because using zero as false is dangerous. That's why strongly typed languages (such as C#) have an actual bool class. So you don't accidentally mix up using an int as a bool when it was supposed to be an int.

                                          N Offline
                                          N Offline
                                          Nikunj_Bhatt
                                          wrote on last edited by
                                          #21

                                          I mostly like loosely typed languages, it decreases programming code. When I program, I know that what type of value the function will accept and return, and I can trap it in anyway by the language's features. So I don't need to know the exact data type.

                                          W 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups