Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

7.06 Arrays

Array Data Types

Arrays provide a means of storing a collection of data in a single variable and accessing each element of the array using an index. Think of it as storing data in a list and you can reference each item in the list using its position in the list. The first element is referenced using 1 as the index. The maximum size of an array is 1,000 elements (items).

Declaring an array is as simple as putting the Array keyword after the data type.

Local NumberVar Array X;

After declaring the array, you need to create it. You have three options before using it. You can create a new array using the MakeArray() function, assign an existing array to it, or re-dimension it using the ReDim keyword. Each of these options are shown in the code sample below.

//Declare an array
Local NumberVar Array X;
Local NumberVar Array Y;
//Create an array with three elements using the MakeArray() function
X := MakeArray(5, 10, 22);
//Rediminsion the array to have 20 elements
Redim X[20];
//Copy an array from one variable to another variable
Y := X;

The ReDim statement changes the number of elements in the array and resets their values. If you don’t want to lose the current, then also use the Preserve keyword.

Redim Preserve X[25]; //Redim an array and preserve existing values

If you want to reference a specific element within the array, Crystal syntax uses square brackets.

//Assign the number 5 into the second element of the array
X[2] = 5;

Assigning values to an array is done in different ways. If you know what the values of an array are during the development process, you can initialize the array with these values using the MakeArray() function. Pass the MakeArray() function all the elements as a comma delimited list and it returns an array that is fully populated with these elements. When using the MakeArray() function you don’t have to specify the array size. Crystal Reports figures that out for you.

As expected, if you want to simply assign a value to an individual element in the array, then the index must be within the array’s bounds. But if you are assigning an entire array to another array variable and they are different sizes, you do not have to redimension the target array. The target array gets overwritten and it will be replaced with the existing array.

//Demonstrate initializing an array and then overwriting it
StringVar Array MonthsInSeason;
MonthsInSeason := MakeArray(“May”, “June”, “July”);
If LongWinter = True Then
MonthsInSeason = MakeArray(“June”, “July”, “August”);

If you don’t know the array values during the development process, you will probably assign the initial values to the array by looping through it. A common way of doing this is using a For Next loop where the range of the loop is the lower and upper bounds of the array. Another common method of assigning values to each element is to do so as the report is looping through its detail records. For each pass through the detail section, you increase the array’s size by one using ReDim Preserve and then you assign the field to the new array element.

//Sample code within the detail section to record customer sales as each record is printed
NumberVar RecordCounter;
NumberVar Array SalesDetail;
RecordCounter := UBound(SalesDetail) + 1;
Redim Preserve SalesDetail[RecordCounter];
SalesDetail[RecordCounter]::= {Customer.Sales};

Once the array is populated, you can test to see if a certain value already exists in the array by using the In operator. Using the In operator saves you the trouble of looping through the entire array searching for a particular value. If the value already exists, the In operator returns True.

Crystal syntax has many predefined functions for summarizing the values in an array. These functions range from summing the total of all the values in the array to getting the maximum value in the array. Table 7-3 lists the array functions.

Table 7-3. Array Summary Functions

Array Function Description
Average(array) Calculate the average of all numbers in the array.
Count(array) Count how many items are in the array.
DistinctCount(array) Count how many unique item there are (no duplicates).
Maximum(array) Return the maximum number in the array.
Minimum(array) Return the minimum number in the array.
PopulationStdDev(array) Return the Population Standard Deviation calculation.
PopulationVariance(array) Return the PopulationVariance calculation.
StdDev(array) Return the Standard Deviation calculation.
UBound(array) Returns the largest available subscript.
Variance(array) Return the Variance calculation.

Although the functions in Table 7-3 are designed to only work with arrays, you can still use them with individual fields. Improvise by creating an array on the fly and passing the function this new array. This is done by creating a new array with the MakeArray() function and passing it the fields to work with.

//Sample code for getting the maximum value of three fields
NumberVar MaxSales1stQtr;
MaxSales1stQtr := Maximum(MakeArray({Sales.Jan}, {Sales.Feb}, {Sales.Mar}));

There are two special functions that only work with arrays of strings. The Join() function concatenates all the elements into a single string. By default it separates each element with a comma. You can override that by passing another delimiter as the second parameter. The corollary function is the Split() function. It takes a delimited string and splits it apart into an array of strings. Just like the Join() function, you can specify a specific delimiter by passing it as the second parameter.

//Demonstrate creating a delimited string from an array and then splitting it back up.
StringVar Array OrigArray;
StringVar Array SplitArray;
StringVar JoinedString;
//Populate the array elements
OrigArray := [“One”, “Two”, “Three”];
//Combine the elements in the array into a single string seperated with “|”
JoinedString := Join(OrigArray, “|”);
//Now split the string apart and put it in a new array
SplitArray := Split(JoinedString, “|”);