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

So now 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

So, 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 thing to do 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])))

End

Erasing Array Element Data

After array elements have data in them, you will sometimes have the 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 0 to SomeContacts[0].Id

Move "" to SomeContacts[0].Name

Since a structured data type is a custom type and can be infinitely complex, so can the process of erasing data in it.

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 unitialized (cannot contain any data).

Erasing Array Data

After arrays have data in them, you will sometimes have the 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:

Contact[] MoreContacts

 

// add code to fill the array with data

 

// now resize the array back to 0 elements

Move (ResizeArray(MoreContacts, 0)) to MoreContacts

Contact[] BlankContacts

Move BlankContacts to MoreContacts

Again, the variable used to clear the data should be unitialized (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:

Contact BlankContact

Move (FillArray(BlankContact, SomeContacts)) to SomeContacts

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

Contact[] BlankContacts

Move BlankContacts to SomeContacts

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

More Array Functionality

You can do a lot more with arrays then 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: