Share your advanced PureBasic knowledge/code with the community.
RobertSF
User
Posts: 61 Joined: Thu May 03, 2018 4:24 pm
Post
by RobertSF » Wed Nov 28, 2018 6:36 pm
Code: Select all
Enumeration
#Item_1
#Item_2
#Item_3
#Item_4
#Item_5
#Total_Entries ; automatically equals 5
EndEnumeration
If you add or remove items, #Total_Entries remains accurate. If your code needs to know the number of items, and you hardcode that number, you'll have to change it if you ever change the number of items. This way, you don't, plus your code is more readable. (e.g. If Entries_Processed = #TotalEntries vs. If Entries_Processed = #5)
Paul
PureBasic Expert
Posts: 1252 Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:
Post
by Paul » Wed Nov 28, 2018 9:10 pm
Or just use the provided #PB_Compiler_EnumerationValue
Code: Select all
Enumeration
#Item_1
#Item_2
#Item_3
#Item_4
#Item_5
EndEnumeration
Total_Entries=#PB_Compiler_EnumerationValue
Also keep in mind if you start your enumeration at a specified value your result won't be the number if items, but the last enumerated value that was used...
Code: Select all
Enumeration 10
#Item_1
#Item_2
#Item_3
#Total_Entries ; value will be 13 (will not be 3)
EndEnumeration
davido
Addict
Posts: 1890 Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK
Post
by davido » Wed Nov 28, 2018 11:19 pm
@
RobertSF ,
You could try this, perhaps:
Code: Select all
dt = #PB_Compiler_Line
Enumeration 10
#Item_1
#Item_2
#Item_3
#Item_4
#Item_5
EndEnumeration
Debug #PB_Compiler_Line - dt - 3
DE AA EB
NicTheQuick
Addict
Posts: 1227 Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:
Post
by NicTheQuick » Wed Nov 28, 2018 11:35 pm
This is also a nice Enumeration trick to get powers of 2:
Code: Select all
Macro enum2pow(_const_)
_const_ = (Bool(#PB_Compiler_EnumerationValue = 0) + Bool(#PB_Compiler_EnumerationValue > 0) * 2 * (#PB_Compiler_EnumerationValue - 1))
EndMacro
Enumeration
enum2pow(#c1)
enum2pow(#c2)
enum2pow(#c3)
enum2pow(#c4)
enum2pow(#c5)
enum2pow(#c6)
enum2pow(#c7)
EndEnumeration
Debug #c1
Debug #c2
Debug #c3
Debug #c4
Debug #c5
Debug #c6
Debug #c7
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Josh
Addict
Posts: 1183 Joined: Sat Feb 13, 2010 3:45 pm
Post
by Josh » Thu Nov 29, 2018 12:07 am
NicTheQuick wrote: This is also a nice Enumeration trick to get powers of 2
For this I use EnumerationBinary
Code: Select all
EnumerationBinary
#c1
#c2
#c3
#c4
#c5
#c6
#c7
EndEnumeration
Debug #c1
Debug #c2
Debug #c3
Debug #c4
Debug #c5
Debug #c6
Debug #c7
sorry for my bad english
Paul
PureBasic Expert
Posts: 1252 Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:
Post
by Paul » Thu Nov 29, 2018 6:03 am
Josh wrote:
For this I use EnumerationBinary
Nice... I totally missed this one in the manual
Josh
Addict
Posts: 1183 Joined: Sat Feb 13, 2010 3:45 pm
Post
by Josh » Thu Nov 29, 2018 6:48 am
Paul wrote: Nice... I totally missed this one in the manual
This is described in the help under Enumeration.
sorry for my bad english
NicTheQuick
Addict
Posts: 1227 Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:
Post
by NicTheQuick » Thu Nov 29, 2018 12:58 pm
Josh wrote: NicTheQuick wrote: This is also a nice Enumeration trick to get powers of 2
For this I use EnumerationBinary
Oh nooo
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
RobertSF
User
Posts: 61 Joined: Thu May 03, 2018 4:24 pm
Post
by RobertSF » Thu Nov 29, 2018 10:45 pm
Wow, thanks everyone! Learned stuff I didn't know.