Wednesday, 9 December 2015

Populating Array in Poeplecode

There are several ways to populate an array. The example code following each of these methods creates the exact same array, with the same elements:

---------------------------------------------------------------------
Use CreateArray when you initially create the array:

Local Array of Number &MyArray;
&MyArray = CreateArray(100, 200, 300);


----------------------------------------------------------------------

Using CreateArray without any parameters creates an array of Any.

Local Array of Any &MYARRAY;

 &MYARRAY = CreateArray();
 &MYARRAY[1] = 100;
 &MYARRAY[2] = 200;
 &MYARRAY[3] = 300;

------------------------------------------------------------------------


Use the Push method to add items to the end of the array:
Local Array of Number &MYARRAY;
 Local Number &MYNUM;

 &MYARRAY = CreateArrayRept(&MYNUM, 0);
 /* this creates an empty array of number */
 &MYARRAY.Push(100);
 &MYARRAY.Push(200);
 &MYARRAY.Push(300);


-------------------------------------------------------------------------

Use the Unshift method to add items to the beginning of the array:
 Local Array of Number &MYARRAY;
 Local Number &MYNUM;

 &MYARRAY = CreateArrayRept(&MYNUM, 0);
 /* this creates an empty array of number */
 &MYARRAY.Unshift(300);
 &MYARRAY.Unshift(200);
 &MYARRAY.Unshift(100);

-------------------------------------------------------------------------

No comments:

Post a Comment