CR Arrays
CR Arrays
Overview
This document is intended to assist you to understand, build, and manipulate
Arrays in the Crystal Reports Designer and the Seagate Info Report Designer.
To better understand Arrays you should be familiar with manual running totals,
and variable scopes. There are technical briefs written on both of these topics
and can be downloaded from our website.
Contents
INTRODUCTION ............................................................................................2
Sample Reports needed ............................................................................... 2
DETAILS ABOUT ARRAYS .............................................................................2
Limitations of Array Elements..................................................................... 2
Assigning values to Arrays .......................................................................... 3
Declaring Numeric Arrays .................................................................................4
Array Specific Functions............................................................................. 5
Array Specific Functions in Detail .....................................................................5
Using Basic Syntax to Create Arrays .................................................................6
Using Arrays ............................................................................................... 6
ARRAYS ......................................................................................................7
Using Arrays to eliminate errors................................................................. 7
CREATING ARRAYS DYNAMICALLY ...............................................................8
Building an Array Dynamically .................................................................. 8
Building Multiple Arrays........................................................................... 10
Passing the Array from Main Report to Subreport ................................... 11
Summing the Array.................................................................................... 11
Sorting the Array manually ....................................................................... 13
Index Report .............................................................................................. 15
CONTACTING CRYSTAL DECISIONS FOR TECHNICAL SUPPORT ....................17
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 1
Crystal Reports Advanced reporting techniques using arrays
Introduction
In Crystal Reports (CR), Arrays are ordered lists of values that are all of the
same data type. Data values in Arrays are also known as elements.
http://support.crystaldecisions.com/downloads
This Winzip file contains the reports listed in this document. These sample
reports will help you better understand how Arrays work. Most of the sample
code contained in this technical brief is based on these sample reports.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 2
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
[1, 2, 3, 4, 5]
In CR versions 6 and 7, you can only declare the Array in a formula and
reference it. You cannot change the elements of an Array as you can in CR
version 8.
For example:
//@MyArray
//assigning values to an Array in Crystal Reports version
//6, and 7
Numbervar Array MyArray := [1, 2, 3, 4, 5];
0
Create a new formula similar to the following to reference values of an Array:
//@ReferenceArray
//referencing the values of elements in the
//variable MyArray
whileprintingrecords;
Numbervar Array MyArray;
If {database.field} = condition1
Then MyArray [2]
Else MyArray [4]
This formula will only return values that meet the condition. For example, if the
database field meets condition1 then the second element of the Array will
display. Otherwise, the fourth element will display.
//@ChangeArray
//changing the values of elements in the variable MyArray
whileprintingrecords;
Numbervar Array MyArray;
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 3
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
If {database.field} = condition1
Then MyArray [2] := 50
Else MyArray [4] := 83
This formula replaces elements in the Array based on the condition. Such as, if
the database field equals the condition then the second element in the Array will
change the value to 50. Otherwise, the fourth element will change to the value
of 83.
//@myArray
//declaring an Array with five elements
Numbervar Array MyArray := [1, 2, 3, 4, 5];
To assign a new value to one particular element of the Array in CR version 8,
create a formula similar to the following:
//@myArray
//Assigning values to one element
Numbervar Array MyArray := [1, 2, 3, 4, 5];
MyArray [3] := 25
The element 3 was reassigned with the value 25.
For example, you attempt to assign a sixth element value in an Array that only
has five elements declared will result in error.
For example:
//@myArray
//Assigning a value to one element not declared
//in the Array
Numbervar Array MyArray := [1, 2, 3, 4, 5];
MyArray [6] := 25
You have not declared the Array to have 6 elements therefore you cannot assign
it a value.
NOTE To increase the number of elements within an Array, you can use either the Redim or
Redim Preserve functions. These functions are available in the Crystal Reports formula
editor using either Crystal Syntax or Basic Syntax.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 4
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
• Redim x[n]
• Redim Preserve x[n]
• Ubound [x]
Array Specific Functions in Detail
Redim x[n]
Using this function will re-dimension the array x to size n, where x is an array
and n is a positive whole number specifying the new size of n.
For example:
//@AddElements
//creates additional elements in array without preserving
//previous values held
StringVar array x := ["a", "bb", "ccc"];
Redim x [4];
//now x = ["", "", "", ""]
The Array will now contain four elements, all of which are blank strings. The
previous string values held within the original positions have all been reassigned
the value of a blank string. This will not preserve the data that was previously
assigned a position within the array.
Example Formula:
//@AddElements
//creates additional elements in array while preserving
//previous values held
StringVar array x := ["a", "bb", "ccc"];
Redim Preserve x [4];
//now x = ["a", "bb", "ccc", ""]
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 5
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
The Array will now contain four elements. All elements previously populated
maintained their original values while the new element has been assigned the
default value for the Array’s data type. This will preserve the data that was
previously assigned a position within the array.
Ubound(x)
This function returns a number containing the largest available subscript for the
given array.
For example:
//CountElements
NumberVar Array x;
Redim x[10];
UBound(x)
//Returns 10
This function is commonly used to obtain the size of an array before using a
looping control mechanism to systematically manipulate elements of the array.
For Example:
//BasicSyntaxArray
//Formula assigns an additional position to the array while
//maintaining previous values stored in other positions
dim myarray(1) as number
counter = counter + 1
myarray = array(counter)
Redim preserve myarray(counter)
myarray(counter) = {Customer.Customer ID}
//Adds a value to the new position and displays it
formula = myarray(counter)
Using Arrays
In CR you are limited to using one value per variable declared. You are able to
store up to 1000 separate values into a single Array. This allows you to pass
many values at one time to a subreport. By passing values to a subreport, you
can dynamically manipulate the values assigned to variables. In addition, this
allows you to sum the elements in an Array within the subreport, after the array
has been systematically manipulated.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 6
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
Arrays
The section goes through each sample report in detail. The sample reports
provided describe possible uses for Arrays. In this section, you will find details
on how to:
• create an Array that loops through a multiple value parameter in the record
selection formula.
• dynamically assign values from a report to an Array.
• pass an Array created in a subreport to a main report and display the
contents of that Array in the main report.
• use the elements in an Array to create a ‘sum’ in a formula.
• use Arrays to create an index for a report.
• use Arrays to create a ‘Top N’ report that can be sorted by a field other than
the one for ‘Top N’.
• use Arrays to suppress blank subreports without generating any white space.
{MyDatabase.Myfield} IN {?Parameter}
This formula is not allowing for spaces in the parameter name. Therefore, the
report skips records in the database without spaces.
To return the complete record set, including the parameter entry containing a
zero, you can modify the record selection formula to read similar to the
following:
numbervar counter;
numbervar positionCount:=count({?test});
stringvar Array NewArray;
while positionCount >= counter do
(
counter:=counter + 1;
redim preserve NewArray[counter];
NewArray [counter] := trim({?test}[counter])
);
{Customer.Customer Name} in NewArray
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 7
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
Note The code provided in this example is commented thoroughly in the sample report called
CR8_Looping_Through_Multiple_Value_Parameter.rpt contained in the Winzip file,
CR_Arrays_Samples.zip.
• Create a counter
• Create an endpoint for the loop
• Create a storage Array
The formula counts how many items have been entered into the multiple-value
parameter. Then it loops through all of the selections entered in the parameter,
trims each one, and assigns the trimmed entry to an element in the new Array.
Now, the record selection is based on the contents of the New Array.
Note If your database is large, and you create a record selection similar to this example, CR
will be forced to bring back all of the records from the database and filter them on the
client side. This can significantly impact the performance of your report.
Example Formula:
//@DynamicArrayBuilder
whileprintingrecords;
numbervar Counter;
//The line below ensures that only new values are added to
//the array.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 8
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
(Counter := Counter + 1;
//The line below ensures that the size of the array does
//not exceed 1000 values. An array can contain a maximum
//of 1000 values.
MyArray[Counter] := {MyDatabase.ValueForArray}));
Figure 1 - Only new values are added to the array. The number of values in the
array is increased and the counter is incremented by one. The
formula displays the new array values.
Note The code provided in this example is commented thoroughly in the sample report called
CR_Dynamic_Array_Builder_Crystal_Syntax.rpt contained in the Winzip file,
CR_Arrays_Samples.zip.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 9
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
Example Formula:
//@Array Generation, Crystal Syntax
whileprintingrecords;
//In this example, up to 3 arrays are populated
//dynamically. If groups have more than 3000 unique
//records, more arrays can be added.
numbervar array idarrayCS;
numbervar array idarrayCSII;
numbervar array idarrayCSIII;
//rt1CS is a counter that will be used to assign the
//element number (array position) of a value in an array.
numbervar rtICS;
//The code below resets the array on the group level.
//Arrays are reset to have 1 element which equals 0...an
//array must have at least one element and this is why the
//array's size is set to 1 element.
if onfirstrecord or {Customer.Country} <>
previous({Customer.Country}) then
(redim idarrayCS[1];
redim idarrayCSII[1];
redim idarrayCSIII[1];
rtICS := 0);
//The code below verifies to see if the Order ID field is
//in any of the above-declared arrays. If it is, it is not
//added again and rt1 is not incremented.
if not({Orders.Order ID} in idarrayCS or {Orders.Order ID}
in idarrayCSII or {Orders.Order ID} in idarrayCSIII) then
(
rtICS := rtICS + 1;
//Redim means that the size of the array (the number of
//elements) is changed to the value at the right.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 10
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
Note The code provided for this example is commented thoroughly in the sample report called
CR_Multiple_Arrays_Dynamically_Populated.rpt contained in the Winzip file,
CR_Arrays_Samples.zip. To view the code of the above formula in Crystal syntax or in
Basic syntax, edit the applicable @Array Generation formula. Also, there is a manual
running total formula, @Manual RT, in the Details section that indicates how many
elements per group are added to all of the arrays.
It is possible to share an Array just like you can share a variable. The following
syntax enables you to share the Array:
Note The code provided in this example is commented thoroughly in the sample report called
CR_Pass_Values_From_Main_Report.rpt contained in the Winzip file,
CR_Arrays_Samples.zip.
If you have a numeric Array with 10 elements and want to find out the sum of
the first five elements, create a formula similar to the following:
//@SumFirstFiveElements
//summarizes the first five positions in the Array
Whileprintingrecords;
numbervar Array MyArray;
sum (MyArray[1 to 5])
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 11
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
For example, you want to see the total Sums of Last Year’s Sales for various
companies. You want to know how the top third, middle third, and bottom third
performed last year. You have already dynamically built an Array and named it
MyArray.
The following code allows you to view the top, middle, and bottom third of the
Sum of Last Year’s Sales for various companies:
{@FirstThirdTotal}
whileprintingrecords;
numbervar Array MyArray;
numbervar counter;
numbervar thirds;
thirds:=truncate((counter / 3), 0);
sum (MyArray[1 to thirds])
{@SecondThirdTotal}
whileprintingrecords;
numbervar Array MyArray;
numbervar counter;
numbervar thirds;
thirds:=truncate((counter / 3), 0);
sum (MyArray[(thirds + 1) to (thirds * 2)])
{@LastThirdTotal}
whileprintingrecords;
numbervar Array MyArray;
numbervar counter;
numbervar thirds;
thirds:=truncate((counter / 3), 0);
sum (MyArray[(thirds * 2 + 1) to counter])
Note The code provided in this example is commented thoroughly in the sample report called
CR_Manual_Running_Array.rpt contained in the Winzip file, CR_Arrays_Samples.zip.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 12
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
Note CR has a limit of 30,000 loop evaluations per formula. Only Arrays that have a maximum
of 150 elements should be sorted using this method.
2. Replace the Array_Name variable with the variable name of your Array.
3. Insert the formula into a section in your report. Ensure that you insert the
formula in the section after the section that is used to populate your Array.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 13
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
The same logic can be used to sort the elements of a string Array:
3. Replace the Array_Name variable with the variable name of your Array.
4. Insert the formula into a section in your report. Ensure that you insert the
formula in the section after the section, which is used to populate your
Array.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 14
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
Index Report
You can build an Index in CR using an Array. CR does not have any internal
capabilities to create Indexes.
1. Dynamically create two Arrays at the Group Header level. One of the
Arrays must contain the Group Name the other Array must contain the Page
Number that corresponds with the Group.
2. Preview the report. You will see the two Arrays with all of the values
necessary to build the Index. You will also see the counter that will tell you
how many elements are contained in the Array.
3. Insert a subreport in the main Report Footer.
4. Share the Arrays and the counter to extract the fields from the two Arrays.
The subreport needs a database field placed in the Details section. There must
be enough details in our subreport to accommodate the number of elements in
the Array.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 15
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
For example:
Use the CustomerName field and place in the Details section because it
produces enough details lines to support the number of elements in the Array.
From the subreport, complete the following steps to display the elements of the
Array:
1. 1. Create a manual running total for the subreport and call it @subcounter.
This counter will be used to display each element from the Array in turn.
2. Create two formulas. The first formula will display the Group Name and
the second formula will display the Page Number.
//@displayGroupName
//This will display the Group Name
whileprintingrecords;
numbervar subcounter;
shared stringvar Array GroupNameArray;
GroupNameArray[subcounter]
{@displayPageNumber}
//This will display the Page Number
whileprintingrecords;
numbervar subcounter;
shared numbervar Array PageNumberArray;
PageNumberArray[subcounter]
At each detail line, the two formulas will display the element that is located at
the position indicated by subcounter. The subcounter is incremented at each
line. This allows the two formulas to display the elements at the position of the
subcounter.
The Customer Name field that is driving the details section has more records
then there are Array elements. The Index contains unnecessary information. In
order to eliminate the extra information, and only have as many details lines
display that are necessary for the two Arrays, you must suppress and
conditionally suppress sections.
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 16
cr_arrays.pdf
Crystal Reports Advanced reporting techniques using arrays
This ensures that CR will generate as many details lines as there are elements in
the Array because the Array is by definition as long as Counter.
3. Suppress the Customer Name field so you only see the Index.
Note The code provided in this example is commented thoroughly in the sample report called
CR_Index_not_store_and_fetch.rpt contained in the Winzip file, CR_Arrays_Samples.zip.
Self-serve Support:
http://support.crystaldecisions.com/
Email Support:
http://support.crystaldecisions.com/support/answers.asp
Telephone Support:
http://www.crystaldecisions.com/contact/support.asp
7/23/2002 10:45 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 17
cr_arrays.pdf