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!

Confused by AFIELDS()....

Status
Not open for further replies.

tristero

Programmer
Jan 24, 2003
95
US
According to the documentation i have, AFIELDS() is supposed fill an array with a list of all the fields in a given table, as well as the structural info about each field.

but how could i accesss JUST the field names?

EXAMPLE:

if i had a table with 3 columns...
would i get the names by using:
Code:
=AFIELDS(aMyArray,'mytable')
?aMyArray[1]
?aMyArray[2]
?aMyArray[3]
when i do this i get the first field name followed be the first 2 structural components for that field...

how do i skip to the next field name? please help... thank you

Dan Trenz
Ann Arbor, MI
 
Code:
=AFIELDS(aMyArray,'mytable')
?aMyArray[1,1]
?aMyArray[2,1]
?aMyArray[3,1]
 
Or, simpler, just use the FIELD() function:
?Field(1)
?Field(2,'mytable')
?Field(3)
 
pnFileCount = ADIR(paFiles,'*.*')
FOR lni = 1 To pnFileCount
MESSAGEBOX(paFiles[lni,1])
ENDFOR


* Notice the ,1

Jim Osieczonek
Delta Business Group, LLC
 
If, for learning purposes, you did an =AFIELDS(aryFlds) on a table and then did a
DISPLAY MEMO LIKE ary*
You would see on-screen the entire array contents and it would be clear that the Field Name is in the first 'column' for each array 'row'.

mnFieldCnt = ALEN(aryFlds,1)
mcFieldName = aryFlds(i,1)

Good Luck,


JRB-Bldr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
CIOServices@yahoo.com
 
The additional point to take note of is that the command afields() returns a numeric value that represents the number of columns in the table so that these are the same:

for x = 1 to afields(latemp)
?latemp(x,1)
endfor

and

for x = 1 to afields(latemp)
?field(x)
endfor

The benifit to accessing the array is that it is available even when your current workspace is not the original table.

Brian
 
Tristero,

What you were doing in your original example was getting one element at a time.
Code:
=AFIELDS(aMyArray,'mytable')
?aMyArray[1]  && field 1 name
?aMyArray[2]  && field 1 type
?aMyArray[3]  && field 1 length
?aMyArray[4]  && field 1 decimals
&& each field is given 16 elements in the array
?aMyArray[17]  && field 2 name
?aMyArray[18]  && field 2 type
?aMyArray[19]  && field 2 length
?aMyArray[20]  && field 2 decimals
As shown in the other examples here, you do need to specify both the row and column to extract out the data in a structured or formatted fashion:
Code:
=AFIELDS(aMyArray,'mytable')
?aMyArray[1,1]  && field 1 name
?aMyArray[1,2]  && field 1 type
?aMyArray[1,3]  && field 1 length
?aMyArray[1,4]  && field 1 decimals
&& only 4 of 16 columns total shown here
?aMyArray[2,1]  && field 2 name
?aMyArray[2,2]  && field 2 type
?aMyArray[2,3]  && field 2 length
?aMyArray[2,4]  && field 2 decimals
Hmmm, interesting that VFP will let you access a two dimensional array also as a single dimensional one without throwing a fit or error. Quite flexible. There might be a use out there for that behavior sometime, somewhere. I can't think of one right off though.

dbMark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top