Page 1 of 1

using ArraySize: Macros for Max and Count

Posted: Tue Jan 19, 2021 12:03 am
by eck49
A couple of very simple macros, largely to save a bit of typing (I seem to constantly and tediously forget the () when using ArraySize).
For me it also makes me think which I really want.

1. How many elements are there in an array, right now?

Code: Select all

Macro ACount(zArr)
  ArraySize(zArr())+1
EndMacro
2. What is the current highest subscript for an array?

Code: Select all

Macro AMax(zArr)
  ArraySize(zArr())
EndMacro
Of course, the initial A is to distinguish them from the many other contexts of 'Count' and 'Max'.

Re: using ArraySize: Macros for Max and Count

Posted: Tue Jan 19, 2021 10:23 am
by STARGĂ…TE
Please use Brackets in Macros, otherwise the results would be sometimes strange :wink:
Your case:

Code: Select all

Macro ACount(zArr)
  ArraySize(zArr())+1
EndMacro

Define Dim Test1.i(9)
Define Dim Test2.i(9)

Debug ACount(Test1) * ACount(Test2) ; 19, because it is expand to "ArraySize(Test1())+1*ArraySize(Test2())+1"
Corrected macro:

Code: Select all

Macro ACount(zArr)
  ( ArraySize(zArr())+1 )
EndMacro

Define Dim Test1.i(9)
Define Dim Test2.i(9)

Debug ACount(Test1) * ACount(Test2) ; 100, because it is expand to "(ArraySize(Test1())+1)*(ArraySize(Test2())+1)"

Re: using ArraySize: Macros for Max and Count

Posted: Wed Jan 20, 2021 11:18 pm
by eck49
@STARGATE
Thanks. Well spotted, not something which came up in my testing.