Path: utzoo!utgpu!water!watmath!clyde!rutgers!sdcsvax!ucbvax!ARIZONA.EDU!gudeman From: gudeman@ARIZONA.EDU ("David Gudeman") Newsgroups: comp.lang.icon Subject: Substring positions Message-ID: <8712302319.AA15498@megaron.arizona.edu> Date: 30 Dec 87 23:19:17 GMT References: <2321@ihuxy.ATT.COM> Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The ARPA Internet Lines: 29 From: ihnp4!ihuxy!lied@ucbvax.Berkeley.EDU (Bob Lied) .... In an example and a solution in chapter 5 of the book (I assume you know the book I refer to), the size of a substring is calculated with *line[i:j] where I would intuitively expect to use i-j I am inferring from this that string positions are like other languages' pointers in that their numerical values are not to be trusted, and I should limit their use to assignments, comparisons, and defined operations like subscripting. Am I right? Well, not really. String positions are integer positions _between_ the characters of the string, where 1 is the position at the beginning of the string. However, non-positive integers are also valid string positions (with 0 at the end of the string). So if i or j is non-positive, it is not true that "*line[i:j] = j-i" (I assume you meant "j-i" rather than "i-j"). In the example in the book, both i and j are guaranteed to be positive, so "j-i" works. Notice though, that in most languages the positive integers refer to character positions, so the correct formula for the length of a substring from i to j is "j-i+1". Perhaps the authors wanted to avoid confusing readers with this subtlety.