How to check if an Array exists?
Select * from myTable where isnull(myTable.keyField) = .f. into array myArray
if alen(myArray) > 0
** Loop through array and Do processing
end ifIn a situation where records selected by the query are 0 then the array is not created and thus the next statement gives an error.
How can I check if myArray exists before doing any further processing?
in VFP9, TYPE("theVar",1) returns "A" if theVar is an array. In earlier versions, you can use TYPE("theVar[1]"):
varIsArray = TYPE("theVar[1]") # "U"
I ended up using the Type(), but it is nice to know that there is something like _TALLY.
Thanks Eric and CetinBason for your help.
Not for discussion but I wanted to warn you about checking with type().
Type() cannot tell exactly if it's an array (unless used with extra parameter in VFP9).
1) Suppose you have an object which supports collection:
type('oObj[1]') would unlikely to be 'U'
2) You've a prg or procedure named the same way which accepts a number as parameter:
type('myProcedure[1]') would again not be 'U'
Of course you could work around both by making sure you don't have such name collisons.
To check if something is array then you need to check it with a function that operates on arrays. ie:
if type('alen(arrMyArray)') = 'N'
However _Tally is not 100% trustable either. Either with type() or _tally you might get a result as if you had something usefull returned. ie: VFP9 engine returns at least 1 record when you use max()).
LOCAL ARRAY arrMax[1]
SELECT MAX
(maxordamt) FROM customer ;
WHERE cust_id == 'NOTEXISTING' INTO ARRAY arrMax ?
_Tally, ;
TYPE('alen(arrMax)'), TYPE('arrMax[1]'), ;
_Tally > 0 and !ISNULL(arrMax[1])Thanks for the indepth explanation CetinBasoz.
I see your point and I think I am going to try to incorporate type('alen(arrMyArray)') = 'N' in my code.