Skip to content

Working with Struct Arrays

This topic discusses common techniques when working with arrays of structured types (structs). This topic shows you how to perform the same actions with struct arrays as Working with Arrays does with an array of a simple data type.

Previous topics in the Array Types book discussed how to declare various types of arrays and how to get data into those arrays. Now we'll discuss what to do with that data.

Storing Array Data

Let's declare a simple struct that stores a numeric identifier and an associated name:

Struct Contact
    Integer Id
    String Name
End_Struct

Next, we'll declare an array of Contact structs:

Contact[] SomeContacts

Add some data to the array:

Move 3 to SomeContacts[0].Id
Move "John" to SomeContacts[0].Name
Move 2 to SomeContacts[1].Id
Move "Ringo" to SomeContacts[1].Name
Move 1 to SomeContacts[2].Id
Move "George" to SomeContacts[2].Name
Move 4 to SomeContacts[3].Id
Move "Paul" to SomeContacts[3].Name

So, now you have an array with unsorted contact data in it; the data "looks" like this:

Element Id Name
0 3 John
1 2 Ringo
2 1 George
3 4 Paul

Sorting Array Data

You might want to sort the data in the array so that it is in the order you want it to be in, rather than the haphazard order in which it was added to the array. Depending on what you are doing, you may or may not have control over the order in which data is added to an array.

DataFlex has a built-in function for sorting arrays, conveniently called SortArray.

Since you are working with a structured type, you will have to write a custom function that tells SortArray how you want the structured type to be sorted. Since a structured data type is a custom type and can be infinitely complex, it is up to you to determine what "sorting" that type means.

In this case, let's sort the array in Name order. The custom comparison function below compares two Contact structs and returns (GT) if the Name element of the first Contact struct is greater than the Name element of the second Contact struct.

Function CompareContacts Contact Contact1 Contact Contact2 Returns Integer
    If (Contact1.Name > Contact2.Name) ;
        Function_Return (GT)
    If (Contact1.Name < Contact2.Name) ;
        Function_Return (LT)
    Function_Return (EQ)
End_Function

You can then call SortArray like this:

Move (SortArray(SomeContacts, Self, get_CompareContacts)) to SomeContacts

Now you have an array with contact data sorted in Name order; the data "looks" like this:

Element Id Name
0 1 George
1 3 John
2 4 Paul
3 2 Ringo

Finding Array Elements

Another common task with arrays is to find data in them. For example, let's find the array element that contains the Name "John" so that we can determine the Id associated with that name.

DataFlex has a built-in function for searching arrays, conveniently called SearchArray, which returns the index of the first array element (there could be more) that contains the data you are looking for.

Again, since you are working with a structured type, you will have to write a custom function that tells SearchArray how you want the structured type to be searched. One convenience is that SortArray and SearchArray can use the same custom comparison function.

To call SearchArray, you will also need to pass it a single copy of the structured type to search for, which must contain the data being searched for:

Contact TestContact
Move "John" to TestContact.Name

You can then call SearchArray, and it will return the first array element that contains the matching data:

Integer iIndex
Move (SearchArray(TestContact, SomeContacts, Self, get_CompareContacts)) to iIndex
If (iIndex <> -1) Begin
    Send Info_Box ("The Id for 'John' is " + (String(SomeContacts[iIndex].Id)))
End

Erasing Array Element Data

After array elements have data in them, you may need to remove that data again.

To erase data in individual array elements and clear the elements to their blank or original values, you can use two methods:

  • Move a blank value to the array element in question. In the case of a structured type, you will need to do this to the complete type structure. What "blank" is depends on the type in question; for an integer it is 0, for a string it is "".

For example, to erase the data in the first element in the SomeContacts array, you can do this:

Move 0 to SomeContacts[0].Id
Move "" to SomeContacts[0].Name
  • A simpler method is to create a single variable of the structured type and move it to the array element to remove data from:
Contact BlankContact
Move BlankContact to SomeContacts[0]

This will work no matter how complex the structured type is. Of course, the variable used to clear the data should be uninitialized (cannot contain any data).

Erasing Array Data

After arrays have data in them, you may sometimes need to remove that data again.

You can delete array elements to erase data (unless the array is static). Typically, you might reset the number of elements to the original 0 elements.

There are two ways to do this:

  • You can use the ResizeArray function to resize the array to zero elements. Let's try this with a dynamic version of the SomeContacts array:
Contact[] MoreContacts
// add code to fill the array with data
// now resize the array back to 0 elements
Move (ResizeArray(MoreContacts, 0)) to MoreContacts
  • A simpler method is to create a single copy of the array without any data in it and move it to the array to remove data from:
Contact[] BlankContacts
Move BlankContacts to MoreContacts

Again, the variable used to clear the data should be uninitialized (cannot contain any data).

If the array is a static array, you cannot remove any elements from it (the array has to remain the size it was declared as); you can only clear the data in those array elements to their blank or original values. There are two methods for doing this:

  • You can use the FillArray function. For example, to erase the data in each element in the SomeContacts array, you can do this:
Contact BlankContact
Move (FillArray(BlankContact, SomeContacts)) to SomeContacts

Of course, the variable used to clear the data should be uninitialized (cannot contain any data).

  • As before, a simpler method is to create a single variable of the array's type and move it to the array to remove data from:
Contact[] BlankContacts
Move BlankContacts to SomeContacts

Again, the variable used to clear the data should be uninitialized (cannot contain any data).

More Array Functionality

You can do a lot more with arrays than is shown here. DataFlex has numerous built-in functions for working with arrays. You can find these in the Language Reference under Array Functions.

Here are some (but not all) of the things you can do using the array functions:

  • You can use FillArray to initialize a range of array elements to a predefined value (it doesn't have to be used to fill the array with blank data; you can fill it with any data).
  • You can use ResizeArray to resize an array and initialize the newly created elements (if any) to a predefined value (it doesn't have to be used to fill the array with blank data; you can fill it with any data).
  • You can use InsertInArray to insert an element into an array.
  • You can use RemoveFromArray to remove an element from an array.
  • You can use CopyArray to create a new array containing a range of elements from another array.