# Working with Arrays

This topic discusses common techniques when working with arrays of simple data types. This topic shows you how to perform the same actions with arrays of simple data types as Working with Struct Arrays does with an array of a structured 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 string array:

String[] SomeContacts

Add some data to the array:

Move "John" to SomeContacts[0]

Move "Ringo" to SomeContacts[1]

Move "George" to SomeContacts[2]

Move "Paul" to SomeContacts[3]

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

 Element Data 0 John 1 Ringo 2 George 3 Paul

#### Tip: Alternative method for appending data to the end of a dynamic array

You can append values to the end of a dynamic array using Move and passing [-1] as the array indexer.

Procedure Example

String[] Sentence

Move "The" to Sentence[-1]

Move "quick" to Sentence[-1]

Move "brown" to Sentence[-1]

Move "fox" to Sentence[-1]

Move "jumps" to Sentence[-1]

Move "..." to Sentence[-1]

End_Procedure

### 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.

You can call SortArray like this:

Move (SortArray(SomeContacts)) to SomeContacts

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

 Element Data 0 George 1 John 2 Paul 3 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.

You can call SearchArray and pass it the data to look for and it will return the first array element that contains the matching data:

Integer iIndex

Move (SearchArray("John", SomeContacts)) to iIndex

If (iIndex <> -1) Begin

Send Info_Box ("'John' is in array element " + (String(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, move a blank value to the array element in question. 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 "" to SomeContacts[0]

### Erasing Array Data

After arrays have data in them, you will sometimes have the need to remove that data again and clear the whole array.

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.

String[] 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:

String[] 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:

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

Move (FillArray("", SomeContacts)) to SomeContacts

• 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:

String[] BlankContacts

Move BlankContacts to SomeContacts

Of course, 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 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.