Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!ucbvax!GARGOYLE.UCHICAGO.EDU!goer%sophist From: goer%sophist@GARGOYLE.UCHICAGO.EDU (Richard Goerwitz) Newsgroups: comp.lang.icon Subject: RE: string indexing peculiarity Message-ID: <9102261550.AA09774@sophist> Date: 26 Feb 91 15:50:05 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 35 Recently I found out the hard way about something I consider to be an anomaly in string indexing. I consider it an anomaly because it was, at least for me, totally unexpected. To wit: s := "-D" x := (s[3:0] | "default") "Obviously" accessing the 3rd character in a 2-character string should fail and x would get the string "default". It turns out, non-intuitively enough, that I get an empty string, exactly as if I had tried s[1:1]. Icon's string indexing methods are definitely non-intuitive for people who acquire it as a second, third, etc. language (which means virtually everyone). I guess the idea that people need to catch on to is that subscripts deal with string positions, and not the index of characters within strings. What throws people off is that s[3] would work as in some other languages (producing the third character in s). In reality, though, this is shorthand in Icon for s[3+:1], i.e. s[3:4]. Of course, if we want to talk about unintuitive schemes, let's not for- get languages where s[0] indexes the _first_ array element in s. s := "-D" s ? { x := move(3) | "default" } s := "-D" s ? { x := (tab(-3), tab(0)) | "default" } s := "-D" x := ("" ~== s[3:0]) | "default" -Richard (goer@sophist.uchicago.edu)