Search code examples
oracle-databasecrystal-reports

Create Copy of Array with different scope


Is there an easy way to create a duplicate of an array by keeping all the original values but simply changing the array's scope from shared to global?

shared stringvar Array SHARED_ARRAY

global stringvar Array GLOBAL_ARRAY


Solution

  • Copying an array and all of its indexed values is really no different than copying a value from one variable into another. There are some nuances in regards to using arrays to consider though.

    To copy the array you would use the following code in a formula field.

    Shared StringVar Array SHARED_ARRAY;
    Global StringVar Array GLOBAL_ARRAY := SHARED_ARRAY;
    

    Any values in SHARED_ARRAY will be copied to GLOBAL_ARRAY once this formula is parsed. However, here is where the nuances come into play. The first issue is that a formula can't return a non-scalar value. Since an array contains multiple values, it is not scalar. Therefore, the formula above will result in an error because the last line of code returns the entire GLOBAL_ARRAY. To avoid this error you can modify the formula to the following.

    Shared StringVar Array SHARED_ARRAY;
    Global StringVar Array GLOBAL_ARRAY := SHARED_ARRAY;
    GLOBAL_ARRAY[1];
    

    The formula now returns only the first index of the array and is now scalar and will not throw an error. You may want to suppress the formula field though since it will be printing the first indexed value of the array.

    The second nuance I wanted to warn you about is in regards to how the SHARED_ARRAY variable is populated with values. You will want to be certain it has been fully populated with all of the values you want it to contain before the formula above is parsed. This will require some familiarity with the order sections of your report will print so you can be certain to place the above formula field in a section that follows the sections that populate SHARED_ARRAY.