Can I determine if a parameter is an array or not?
-
"#" gives an error on a non-array non-string, so that won't work; I was hoping it would just give zero or one or something. I don't see an "istype" or "isarray" function. Trying to subscript a non-array gives an error, even if wrapped in "exists", so that won't work. So the only thing I see to do is always pass an array if it ever will be an array, and just pass a 1-element array if I don't need more than one item. I'm trying to improve on that. Anything I'm missing?
A while back I put in an enhancement request to have isnan return true for an array rather than an error. That would allow differentiation of scalar numbers from arrays, which is probably most cases, but wouldn't differentiate arrays from other types, like strings. I thought to modify that request somehow, but I'm not sure if I can change it, or what would be a good way to suggest doing this.
-
@DonStauffer if neither of the following works then I can't think of another way at present:
exists(#var.x) exists(var.x[0])
The second one would of course also return false for an array of zero length.
-
@DonStauffer The LiveCode development environment in which I mostly work has automatic variable typing, which is VERY convenient, but you still need to test for arrays using something akin to "if the keys() of [variable] is not empty then". That works without a hitch, but I avoid it by being diligent in appending "A" to the name of any array variable.
-
@MJLew I'm not worried about mistakes. I want to be able to send a parameter which may or may not be an array, on purpose, and detect which in the called macro.
-
@dc42 Both of these produce errors.
;exists(#var.x) ;exists(var.x[0]) var x = {1,2} var y = 3 ; Method 1 if exists(#var.x) ; expected an identifier echo "exists(#var.x)" else echo "! exists(#var.x)" if exists(#var.y) echo "exists(#var.y)" else echo "! exists(#var.y)" ; Method 2 if exists(var.x[0]) echo "exists(var.x[0])" else echo "! exists(var.x[0])" if exists(var.y[0]) ; Cannot index into variable or parameter 'y^' of non-array type echo "exists(var.y[0])" else echo "! exists(var.y[0])"