Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quickly determine if a varable is an array and get dimensions

Status
Not open for further replies.

darrellblackhawk

Programmer
Aug 30, 2002
846
US
[tt]

CLEAR

LOCAL i

LOCAL ARRAY a1[1], a2[1,2], a3[2,1], a4[4,3], a5[43,53]

? IsArray(@i)
? IsArray(@a1)
? IsArray(@a2)
? IsArray(@a3)
? IsArray(@a4)
? IsArray(@a5)

* Results at runtime: (added star)
*0'
*1,1,1
*1,1,2
*1,2,1
*1,4,3
*1,43,53



* Pass the variable by reference to determine if it's an
* array. If it is, return a comma delimited string,
* otherwise returns "0"

FUNCTION IsArray(aSuspect)
RETURN IIF(TYPE(&quot;aSuspect[1]&quot;)<>&quot;U&quot;,&quot;1,&quot; + ;
TRANSFORM(ALEN(aSuspect,1)) + &quot;,&quot; + ;
TRANSFORM(INT(ALEN(aSuspect)/ALEN(aSuspect,1))),&quot;0&quot;)
ENDFUNC
[/tt]

Darrell
 


Doesn't an Array return VARTYPE = &quot;A&quot; (OR TYPE)?

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I'm using VFP6.0 still.

Darrell
 
Mike,

If you use VARTYPE() or TYPE() on an array, they return the variable type of the first element of that array.
At least up to VFP 7. I haven't used VFP 8 yet, it could have changed.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
The purpose of the function is to determine if a
variable passed in by 'reference' is an array, plus
to determine its dimensions if it is an array.

I believe in VFP 8; as Mike posted; VFP returns
'A' for array types.

Darrell

 
In *all* versions of VFP, test the type of the first element:

If Type('lalArray[1]') # &quot;U&quot;
* You've got an array
Endif

If the first element is undefined (Type() = &quot;U&quot;), you don't have an array.
 
Dave,

Are you sure about that? Tested ok for me.

Slighthaze = NULL
craig1442@mchsi.com
&quot;Whom computers would destroy, they must first drive mad.&quot; - Anon​
 
Hmm. Not sure what Iwas doing, but I must have been using just VARTYPE() to test. But after doing the following, the tested the same using TYPE() and VARTYPE():
Code:
DIMENSION zzz[3]
?Vartype(zzz)    &&... returns 'L'
?Vartype(zzz[1]) &&... returns 'L'

zzz[1] = 'A'
zzz[2] = 1
zzz[3] = .T.
?Vartype(zzz)    &&... returns 'C'
?Vartype(zzz[1]) &&... returns 'C'
?Vartype(zzz[2]) &&... returns 'N'
?Vartype(zzz[3]) &&... returns 'L'

But, using VARTYPE() and TYPE() here:
Code:
STORE 'A' TO yyy
?Vartype(yyy)    &&... returns 'C'
?Vartype(yyy[1]) &&... File 'yyy.prg' does not exist

STORE 'A' TO yyy
?Type('yyy')    &&... returns 'C'
?Type('yyy[1]') &&... returns 'U'

It looks like TYPE() works as noted.
Let's go with that.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top