Extended Select

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Extended Select

Post by Jac de Lad »

Hi,
would it be possible and wanted to enhance Select a bit? Coming from XProfan I can use

Code: Select all

Select MyVar
Case 5,<0
Debug "MyVar is 5 or below 0"
Case >10
Debug "MyVar is bigger than 10"
Default
Debug "MyVar is something else"
EndSelect
to better use Select.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Extended Select

Post by BarryG »

If you have a known min/max range, then you can do it with hard-coded values like below. Not exactly the formatting you want, but it works.

I take it you have some reason that you don't want to use If/ElseIf/EndIf to do what you're asking?

Code: Select all

Select MyVar
  Case 5, -999999 To -1
    Debug "MyVar is 5 or below 0"
  Case 11 To 999999
    Debug "MyVar is bigger than 10"
  Default
    Debug "MyVar is something else"
EndSelect
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Extended Select

Post by Jac de Lad »

Hi BarryG,
yeah that would work, if I know both boundaries. On the other hand I could set second value ridiculously high/low, this would work for most of the time too.
I want to avoid if, because I have some statements where I react to many, many values of a variable. Font know if if would fe faster though...
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Extended Select

Post by BarryG »

Jac de Lad wrote:Dont know if if would be faster though
Apparently it's the same speed when you check the compiled source -> viewtopic.php?p=28350#p28350

But Case has one additional assembler command ("PUSH [v_a]"), so technically Case is slower than If/ElseIf/EndIf.
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Extended Select

Post by Jac de Lad »

Ah, good to know. Thanks.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Extended Select

Post by infratec »

In such a case I use if

Code: Select all

If MyVar = 5 Or MyVar < 0
  Debug "MyVar is 5 or below 0"
ElseIf MyVar > 10
  Debug "MyVar is bigger than 10"
Else
  Debug "MyVar is something else"
EndIf
User avatar
Jac de Lad
Enthusiast
Enthusiast
Posts: 106
Joined: Wed Jul 15, 2020 7:10 am
Contact:

Re: Extended Select

Post by Jac de Lad »

Yeah of course. I just wanted to make it a bit easier with Select.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Extended Select

Post by mk-soft »

Select is extended... :wink:

Code: Select all

For a = 0 To 10
  Select a
    Case 1, 3 To 7, 9
      Debug "Case 1, 3 To 7, 9 / a = " + a 
    Case 2, 8
      Debug "Case 2, 8 / a = " + a
    Default
      Debug "Default / a = " + a
  EndSelect
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply