naz@hslrswi.hasler.ascom.ch (Norman H. Azadian) (02/26/91)
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]. Thinking about it, I have to admit that it makes a certain amount of sense. In fact, it makes a lot of sense when I think about it. However it was totally unexpected and so I thought I'd mention it to perhaps help some other hapless soul from falling into the same pit. NHA -- PAPER: Norman Azadian; Ascom AG; Belpstrasse 23; 3000 Berne 14; Switzerland INTERNET: naz%hslrswi.uucp@uunet.uu.net UUCP: ...{uunet,ukc,mcvax,...}!chx400!hslrswi!naz VOICE: +41 31 63 2178 BITNET: naz%hslrswi.UUCP@cernvax.BITNET
goer%sophist@GARGOYLE.UCHICAGO.EDU (Richard Goerwitz) (02/26/91)
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)