CASE ranges in SELECT

BlitzMax Forums/BlitzMax Programming/CASE ranges in SELECT

Scaremonger(Posted 2007) [#1]
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?


GfK(Posted 2007) [#2]
Think this should work:
select char
  case char >= 65 and char <= 90
    'stuff
  case char >= 97 and char <= 122
    'more stuff
end select



Chris C(Posted 2007) [#3]
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...


GfK(Posted 2007) [#4]
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



Scaremonger(Posted 2007) [#5]
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...


CS_TBL(Posted 2007) [#6]
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.


GfK(Posted 2007) [#7]
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...
Er... that isn't my code.

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



CS_TBL(Posted 2007) [#8]
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



GfK(Posted 2007) [#9]
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.


CS_TBL(Posted 2007) [#10]
hm... odd. is S/C always faster, or does it depend on the conditions and amount of choices?


Scaremonger(Posted 2007) [#11]
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?


Grey Alien(Posted 2007) [#12]
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!