using ArraySize: Macros for Max and Count

Share your advanced PureBasic knowledge/code with the community.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

using ArraySize: Macros for Max and Count

Post 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'.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
STARGÅTE
Addict
Addict
Posts: 2234
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: using ArraySize: Macros for Max and Count

Post 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)"
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: using ArraySize: Macros for Max and Count

Post by eck49 »

@STARGATE
Thanks. Well spotted, not something which came up in my testing.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply