Page 1 of 1

Simple, little trick with enumerations

Posted: Wed Nov 28, 2018 6:36 pm
by RobertSF

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)

Re: Simple, little trick with enumerations

Posted: Wed Nov 28, 2018 9:10 pm
by Paul
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

Re: Simple, little trick with enumerations

Posted: Wed Nov 28, 2018 11:19 pm
by davido
@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

Re: Simple, little trick with enumerations

Posted: Wed Nov 28, 2018 11:35 pm
by NicTheQuick
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

Re: Simple, little trick with enumerations

Posted: Thu Nov 29, 2018 12:07 am
by Josh
NicTheQuick wrote:This is also a nice Enumeration trick to get powers of 2
For this I use EnumerationBinary :mrgreen:

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

Re: Simple, little trick with enumerations

Posted: Thu Nov 29, 2018 6:03 am
by Paul
Josh wrote: For this I use EnumerationBinary :mrgreen:
Nice... I totally missed this one in the manual :D

Re: Simple, little trick with enumerations

Posted: Thu Nov 29, 2018 6:48 am
by Josh
Paul wrote:Nice... I totally missed this one in the manual :D
This is described in the help under Enumeration.

Re: Simple, little trick with enumerations

Posted: Thu Nov 29, 2018 12:58 pm
by NicTheQuick
Josh wrote:
NicTheQuick wrote:This is also a nice Enumeration trick to get powers of 2
For this I use EnumerationBinary :mrgreen:
:lol: Oh nooo :P

Re: Simple, little trick with enumerations

Posted: Thu Nov 29, 2018 10:45 pm
by RobertSF
Wow, thanks everyone! Learned stuff I didn't know.