Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Jac de Lad
Enthusiast
Posts: 106 Joined: Wed Jul 15, 2020 7:10 am
Contact:
Post
by Jac de Lad » Fri Oct 16, 2020 7:45 pm
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
Posts: 3292 Joined: Thu Apr 18, 2019 8:17 am
Post
by BarryG » Sat Oct 17, 2020 2:11 am
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
Jac de Lad
Enthusiast
Posts: 106 Joined: Wed Jul 15, 2020 7:10 am
Contact:
Post
by Jac de Lad » Sat Oct 17, 2020 2:17 am
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
Posts: 3292 Joined: Thu Apr 18, 2019 8:17 am
Post
by BarryG » Sat Oct 17, 2020 2:31 am
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.
Jac de Lad
Enthusiast
Posts: 106 Joined: Wed Jul 15, 2020 7:10 am
Contact:
Post
by Jac de Lad » Sat Oct 17, 2020 2:42 am
Ah, good to know. Thanks.
infratec
Always Here
Posts: 6817 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sat Oct 17, 2020 5:24 pm
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
Jac de Lad
Enthusiast
Posts: 106 Joined: Wed Jul 15, 2020 7:10 am
Contact:
Post
by Jac de Lad » Sat Oct 17, 2020 5:51 pm
Yeah of course. I just wanted to make it a bit easier with Select.
mk-soft
Always Here
Posts: 5335 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » Sat Oct 17, 2020 5:58 pm
Select is extended...
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