CASE ranges in SELECT
BlitzMax Forums/BlitzMax Programming/CASE ranges in SELECT
| ||
Hi, My first post! I've found the forums very useful, and with a bit of research I've been able to sort out all my questions until now... In VB/ASP/VBS etc I could do something like this.. function isChar%( char$ ) select asc( char ) case 65 to 90, 97 to 122 '# Alpha chars return true end select end function I have tried changing the case statement thus: case 65..90, 97..122 but still fails. I cannot find anything in the documentation, or on these forums. Is this possible? |
| ||
Think this should work:select char case char >= 65 and char <= 90 'stuff case char >= 97 and char <= 122 'more stuff end select |
| ||
you can only do case 65,67 which would get only 65 and 67 you'd be better of with a bunch of if statement... |
| ||
Ah, knew there was a way of doing it but my code above was slightly wrong. Just checked in my game where I use it. Modified/corrected code below:char = 70 Select True Case char >= 65 And char <= 90 DebugStop 'stuff Case char >= 97 And char <= 122 DebugStop 'more stuff End Select |
| ||
Thanks GFK, but that does not appear to work.Print "is a=" + isChar("a") '# Returns 0 Print "is A=" + isChar("A") '# Returns 0 Print "is 9=" + isChar("9") '# Returns 0 Function isChar%( char$ ) Select Asc( char ) Case char >= 65 And char <= 90 ; Return True Case char >= 97 And char <= 122 ; Return True End Select End Function Seems a shame to go back to IF's... |
| ||
Why? IF's are ideal for large chunks like checking for >=65 and <=90. Select-case is ideal for seemingly random options, like 1, 52, 4100, 130, 245, 5, 6, 909 etc. |
| ||
Thanks GFK, but that does not appear to work. Er... that isn't my code.Print "is a=" + isChar("a") '# Returns 0 Print "is A=" + isChar("A") '# Returns 0 Print "is 9=" + isChar("9") '# Returns 0 Function isChar%( char$ ) Select Asc( char ) Case char >= 65 And char <= 90 ; Return True Case char >= 97 And char <= 122 ; Return True End Select End Function Seems a shame to go back to IF's... Your function is going to return True whatever condition is met. You have no way of distunguishing between upper and lower case from this. Try this: Function isChar%( char$ ) local c:int = Asc( char ) Select True Case c >= 65 And c <= 90 ; Return 1 Case c >= 97 And c <= 122 ; Return 2 Default ; Return 0 End Select End Function |
| ||
But to get back on the IF vs SELECT issue, using IF is even shorter!Function isChar%( char$ ) Local c:int = Asc( char ) If c >= 65 And c <= 90 Return 1 If c >= 97 And c <= 122 Return 2 Return 0 End Function |
| ||
I've tested Select/Case and If/EndIf over ten million iterations. Select/Case: 2,455ms If/EndIf: 2,825ms Select/Case is therefore faster. The shortest solution is not necessarily the best. |
| ||
hm... odd. is S/C always faster, or does it depend on the conditions and amount of choices? |
| ||
Sorry GFK, I must have posted at the same time as you - (That was my code not yours) I've never seen a select True statement before. That is rather odd, but if it works - don't knock it. Is that documented anywhere? |
| ||
yeah select true is very useful. I'm sure it's documented as found out about it - perhaps it was via the fourm though, or maybe it was from the BlitzPlus manual! |