a bit of array related confusion

I am having trouble figuring out how you increment arrays. To illustrate what I mean, I want to show how easily it is done in Visual dBase. You declare a = new array (); then you can say a.add ("Andy Kramer"); the next statement might be a.add("Cindy"). You've got a one dimensional array with two elements in it. Naturally, array is an object.

Here you've got AINS() which inserts an element in an array kicking out the last element which is a nasty surprise, I should say. How do you add an element to an array I want to add something to the LAST element, create (n+1)th element.

In my particular task at this moment I will make do with COPY TO ARRAY command. I want to store values of just one field in a table into an array. This operation is loopy. My question is: during the next iteration in the FOR...ENDFOR cycle the information will be ADDED ( ) to this array or a NEW array will be created for this purpose I prefer the last option. If the information is going to be ADDED, then I want to be able to destroy the array from the previous cycle. How do you destroy an array Do you use RELEASE array command for it every time

Thanks.

 




Answer this question

a bit of array related confusion

  • JM Willkie

    Thanks, much appreciated.

  • Marius Gheorghe

    Thanks, Cetin. It is a wealth of information for me. A very practical information, indeed.

  • DaveIII

    Also a related question: if I deleted an array elements with ADEL() command how do I change the size of the array for all practical purposes.

    Thanks.



  • Qing Fang

    To change the size of an array, use the DIMENSION command. This command adds one element at the end of a one-dimensional array:

    DIMENSION aMyArray[ ALEN(aMyArray) + 1 ]

    Arrays in VFP are _not_ objects. You may find the paper title "You Need Arrays" on my website at http://www.tomorrowssolutionsllc.com/session_materials.htm useful.

    Tamar

  • Clumsi

    You're right that array operations in VFP has always been confusing with ains(), adel(). However once you master them they work exceptionally well. You should keep in mind they don't resize the array and trailing of elements depends on if it is for row or col. To resize an array you would use 'dimension' command or a command that resizes the array (those whose destination is array - ie: SQL select).

    The way arrays being an object as you showed with add/remove/indexer methods is actually a collection class available in VFP9 (as in other languages like VB and .Net).

    For the task you described however you don't need loop. It's just a single command:

    copy field myField to array aMyArray

    aMyArray doesn't exist and copy to command creates and sizes it accordingly. If it existed then copied rowcount is the same as arrays rowcount. ie:

    local array aMyArray[reccount(),1] && 1 is important making this array 2 dimensional
    copy field myField to array aMyArray

    Yet another practical way:

    local array aMyArray[1], aYourArray[1]
    select myField from myTable into array aMyArray

    select myField1, myField2 from myTable into array aYourArray

    If select returns one or more more rows it sizes the array as it needs to be (recreates internally to match rows/columns). If no rows are retirned doesn't touch existing array.

    I rarely use release for any variable including arrays. Declaring your variables "local" VFP does the house keeping for you, basically.

    In VFP9 collection class exists and it is an object working the way you described.


  • a bit of array related confusion