Buddyscript has objects, but internally they are n-dimensional arrays of strings. They can have properties, like real objects, but have some limitations.
One I've come across recently was that the Buddyscript "for each" can't iterate a non-list object (a scalar), and because of code restrictions, we had to be able to handle both looping scalars and lists. Of course this can be done with a simple "if" that uses the Count() function and behaves differently if we have one or N elements.
But the Count function is a bit fragile. If the variable passed is not a list it will throw an exception. So I've made a small "safe count" function to allow counting scalars (as 1), lists and empty scalars (as 0):
function SCount(LIST)
if (!Exist(LIST))
return 0
if (LIST == "")
return 0
if (IsObject(LIST))
return Count(LIST)
else
return 1
This is a small test-pattern I made to test it:
+ _test_scount
VAR1 = ""
VAR2 = ""
VAR2.EL = ""
VAR3 = ""
VAR3.EL = 1
VAR4 = ""
VAR4[0] = 1
VAR5 = ""
VAR5.EL = ""
VAR5.EL[0] = 1
VAR6 = ""
VAR6.EL = ""
VAR6.EL[0] = 1
VAR6.EL[1] = 2
VAR6.EL[2] = 3
RES = SCount(VAR1)
- var1: RES
nop
RES = SCount(VAR2.EL)
- var2.el: RES
nop
RES = SCount(VAR3.EL)
- var3.el: RES
nop
RES = SCount(VAR4)
- var4[0]: RES
nop
RES = SCount(VAR5.EL)
- var5.el: RES
nop
RES = SCount(VAR6.EL)
- var6.el: RES
And this are the output results:
var1: 0
var2.el: 0
var3.el: 1
var4[0]: 1
var5.el: 1
var6.el: 3
Tags: Development