PeopleCode Array

In a previous post I talked about creating an array so as to utilize a PeopleCode Grid Column function.  Another developer and I recently struggled with getting a multi-dimensional array to work and the proverbial lightbulb went off.

A single dimensional array is usually described something like this in your typical CS class:

(“The”,”big”,”brown”,”dog”,”licked”,”his”,”face.”/n)

In fact, if you have a C background, you’d recognize that strings are really array’s of char.  Like above.  I digress.

In PeopleCode an array is declared and created:

local array of any &ary;
&ary = CreateArray();
&ary.Push(“The”);

Each element is then pushed into the array.  Going back to the CS style of nomenclature, a multi-dimensional array should look something like this:

{[(),(),(),(),(),()],[(),(),(),(),(),()]…}

In the aforementioned discussion with the other developer I had referenced the linked article – began diagramming what he wanted to do and started to really think hard about the PeopleCode Array class specification.

Use the CreateArrayRept function to create an array that contains count copies of val. If val is itself an array, the created array has one higher dimension, and each element (sub-array) is the array reference val.

The type of the first parameter (val) determines the type of array that is built. That is, if the first parameter is of type NUMBER, an array of number is built. If count is zero, CreateArrayRept creates an empty array, using the val parameter for the type.

If you are making an array that is multi-dimensional, val will be the subarray used as the elements.

Doh!

Here’s a concrete example.  The other developer wanted a multi-dimensional array with six elements to each sub-array.  Clear as mud right now but hold on…

multi-dim

If you imagine the outcome as a grid (or Excel) then it becomes clear.  He wants six unique items (cells) to a row (index).

multi-dim2

Each row in the above picture contains a group of six ‘things’.  Another way of saying that the above represents an array of length three, and each index/element of the array contains another array.

Or, to reiterate the above snippet from PeopleBooks… if the value val is itself an array, then the created array has one additional dimension.  The array value indicated by the term val is an element of that array.

What we then did was this:

local array of array of string &ary;
&ary = CreateArrayRept(CreateArray(“”,6), 0);
&ary.Push(CreateArray(val1,val2,val3,val4,val5,val6));
&ary.Push(CreateArray(val1,val2,val3,val4,val5,val6));

This will end up with something that conceptually looks like:

Row #1: val1 val2 val3 val4 val5 val6
Row #2: val1 val2 val3 val4 val5 val6

And so on.

Update:  I changed the above code because we couldn’t get it to work using any as the array type. And since we defined it this way:

CreateArrayRept(CreateArray(), 6);

Seven ‘rows’ (or elements each containing an array of any) were created.  The correction comes from working code.  That’s not a typo – while six were specified seven were created (one higher dimension being created since the value is itself an array).

Advertisements

About Lee Greffin
Just another programmer...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: