Articles - False array

Creating a False Array


Arrays really are powerful things. That's what makes Oracle's oversight of these things so surprising. Now, I have an example elsewhere on this site of creating a C business function to manage a true array. However, this might be a fairly heavy-handed approach for a lot of people and maybe unnecessary in certain circumstances.

For instance, let's imagine you want to simply count the number of distinct items on a sales order. You could create a C business function to manage an array.  For each item you come across in the sales order detail you would refer back to the array to see if the item has already been processed. If the item is not presently in the array you add it to the array and then increment the item counter. Unfortunately, this could take you the greater part of a day to simply write the business function and test it. For something so simple as counting items I've devised an even simpler solution.

What we really care to know is whether we've run across this item number previously. We need some means to save the item numbers that we've processed so that we can refer back to this list. To this end, we simply need a list and what simpler object is there than a 'string' variable.

So this is what we're going to do: we're going to create a very long string and every time we run across a unique item number we are going to add it to the string. As we add items to the string we will increment our item counter. However, if we find that our item number is already in the string we will not increment the item counter. It's that simple, and it can be done with only a few lines of code.

Now look at the code below. First we create a very long string. In this case I have created an ER variable named: evt_ItemArrayString_DESC2000 . It's based on the data dictionary field DESC2000 and is 2000 characters long. We can fit about 222 short item numbers into a string that's this long. Obviously, this technique won't work for extremely large data sets, but in the case of this solution I don't have to worry about that much data.

The Code


 // Accumulate the number of Items in the order. 
// --Determine if need to add item to fake index
 VA evt_ItemAsString_A801 =  concat(",",concat([evt_IdentifierShortItem_ITM],",")) 

// If the item is not in the String, add it
Is String In String
VA evt_ItemAsString_A801 -> szStringToFind
VA evt_ItemArrayString_DESC2000 -> szFromString
VA evt_StringIsInString_EV01 <- cWasStringFound
 If VA evt_StringIsInString_EV01 is not equal to "1" 
If VA evt_ItemArrayString_DESC2000 is less than or equal to <Blank>
VA evt_ItemArrayString_DESC2000 = ","
End If

VA evt_ItemArrayString_DESC2000 = concat([VA evt_ItemArrayString_DESC2000],
VA evt_ItemArrayString_DESC2000 = rtrim([VA evt_ItemArrayString_DESC2000],' ')
VA evt_ItemArrayString_DESC2000 = concat([VA evt_ItemArrayString_DESC2000],",")
BF mnNumberItems = [BF mnNumberItems]+1
End If


Back to the code: We place our Short Item Number into a string variable, then we look for our item number within the long string.  We do this using the business function "Is String in String". The function will return a '1' if it finds our item number within the string (note that we converted our item number to a string variable at the top and placed 'commas' on each side). If we don't find our item number in the long string we know that we have not processed this item before. If this is the case, we simply concatenate our item number to the string (thus adding the item to our 'list') and follow that with a comma to separate our newly added value from any prior values. We then increment our item count variable. The next time this item is encountered it will be found within the long string and the item counter will not increment.

And that's the whole solution. We've created a functional array, albeit limited in size and without a subscript, but very useful when we know that our data set will not be extremely large.

example graphic