Re: if statement not working
- Posted by Kat <gertie at PELL.NET> Mar 22, 2002
- 537 views
On 22 Mar 2002, at 18:03, Derek Parnell wrote: <snip> I just use equal() on everything now. <snip> > >2. Using slices on strings. This "gotcha" was a handmine that littered a lot > >of > >my early code. This initially looks like a good idea, until you get a string > >shorter than you expected. So you write: > > > > if s[1..3] = "/* " > > > >and the code goes into cardiac arrest if the length of s is less than 3. If > >nothing else, Euphoria should offer a substring function that would provide > >some safety: > > > > if substr( s, 1, 3 ) = "/* " > > > >Of course, it would have to pad the string that's returned with nulls so the > >length would match what was expected, or else *boom*. > > > >Adding short-circuiting helps, but still, I think it's a nasty trap that > >could easily turn someone away from using Euphoria. > > I just tripped against this stupid restriction again last night. I've written > a > function called begins() to help me out, but it ain't pretty. > > if begins(s, t) then ... > > I originally wrote: > > if equal(s[1..length(t)], t) then ... > > but this fails whenever s is shorter than t. This, to me, is a no-brainer. As much of s should be returned as possible, up to length(t), so what if you ask for more than what is there? This, and the equal() thing, is why i mentioned mirc yr(s) ago, even if you consider it a non- language, it runs without crashing in that example. You toss enough of these features at a newbie, and they will leave. I was just at a standstill in Pascal, and C, and not getting anywhere with anything else at the time, i was frustrated enough to stay with Eu longer than normal. Kat