Re: if statement not working
- Posted by Derek Parnell <ddparnell at bigpond.com> Mar 21, 2002
- 535 views
22/03/2002 5:04:01 PM, David Cuny <dcuny at LANSET.COM> wrote: > >Euphoria isn't C, or Pascal, or any other language. That's a good thing. But >it doesn't exist in a vaccum, either. And newbies (and myself) seem to be >tripped up by two features in Euphoria. > >1. The '=' sign. I've argued against the current implementation until I was >blue in my face. I think the current behavior is more likely to result in >buggy code. In what other language does an '==' cause the program to come to >a hard stop? I can see where compare() came from. In 'C' one has to use strcmp() for a good reason, because strings, and arrays in general, are really pointers. So: char *s, *t; if (s == t) // actually checks to see if 's' and 't' are literally addressing the same RAM location. but if (strcmp(s,t) == 0) // is calling a function to examine each byte in the character arrays However, as David points out, Euphoria ain't 'C'. In Eu, there are no pointers. So there can be no confusion about '=' referring to either the address or contents of a sequence. It has to be the contents because you cannot reference its address. >The only good thing about this is that it prepares you for C coding, by using >compare() every time you deal with a string. After a while, it becomes second >nature. And of course, we should all be preparing to abandon Eu for C >But Robert sees this as a feature, and it's not going to change. Go figure! >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. (I'm writting a simple text adventure game to highlight a number of new additions to win32lib. If things go well, I'll have it operational by the end of the weekend.) ------- Derek.