Microsoft KB Archive/104053

= Description of the differences between COPY TO ARRAY and SELECT INTO ARRAY in FoxPro =

Article ID: 104053

Article Last Modified on 3/10/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft FoxPro 2.5b
 * Microsoft FoxPro 2.5a
 * Microsoft FoxPro 2.0
 * Microsoft FoxPro 2.5b for MS-DOS
 * Microsoft FoxPro 2.5a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q104053



SUMMARY
Once an array has been created with the COPY TO ARRAY command, the array length remains constant even when a subsequent COPY TO ARRAY command is issued. However, the SELECT INTO ARRAY command creates a new array each time it is issued.



MORE INFORMATION
The following steps illustrate this behavior using the FoxPro 2.x CUSTOMER table:  In the Command window, issue the following commands:

SET DEFAULT TO 

USE customer

  Open the Debug window, and type ALEN(arr). Observe the value displayed after issuing each of the following commands: COPY TO ARRAY arr FIELDS state FOR state = "CA" * arr has 123 elements, all of which are "CA".

COPY TO ARRAY arr FIELDS state FOR state = "AK" * arr still has 123 elements, but issuing the command * DISPLAYMEMORY LIKE arr will show that only the first * element of arr is "AK"; the rest are "CA". Use this code for Visual FoxPro: USE HOME(2)+"data\customer" COPY TO ARRAY arr FIELDS country FOR country = "USA" * arr has 13 elements, all of which are "USA". DISPLAY MEMORY LIKE arr TO FILE arr1.txt noconsole

COPY TO ARRAY arr FIELDS country FOR country = "Brazil" * arr still has 13 elements, but issuing the command * DISPLAYMEMORY LIKE arr shows that only the first * 9 elements of arr is "Brazil"; the rest are "USA".
 * RELEASE arr && release array arr and new aray is created.

DISPLAY MEMORY LIKE arr TO FILE arr2.txt noconsole MODIFY COMMAND arr1.txt NOEDIT nowait MODIFY COMMAND arr2.txt NOEDIT NOWAIT NOTE: If the second COPY TO ARRAY command returns a smaller number of elements than those existing in the array, only that number of elements will be overwritten; the others will retain their former value.

If the second COPY TO ARRAY command returns a larger number of elements than those existing in the array, the result set will be truncated to the number of elements in the array.

To obtain correct results, you must use the RELEASE command to release the array between consecutive COPY TO ARRAY commands.   Observe the value of ALEN(arr) after issuing the following commands: SELECT cno FROM customer INTO array arr WHERE state = "CA" * arr now has 123 elements, all of which are "CA".

SELECT cno FROM customer INTO ARRAY arr WHERE cno = "AK" * ALEN(arr) =1; the value is "AK". Use this code for Visual FoxPro: USE HOME(2)+"data\customer" SELECT country FROM customer INTO array arr WHERE country = "USA" * arr now has 13 elements, all of which are "USA". ? arr(1) && the value is "USA". ? ALEN(arr) && 13 SELECT country FROM customer INTO ARRAY arr WHERE country = "Brazil" * arr now has 9 elements, all of which are "USA". ?Arr(1) && the value is "Brazil". ?ALEN(arr) && 9 

