SPL Series and Arrays

 

Unlike C/C++, series are a primary data type in SPL. A series may be processed much like a scalar value. For example:

 

a = {1, 2, 3, 4};

b = a+1;

c = b^2;

 

b == {2, 3, 4, 5}

c == {4, 9, 16, 25}

 

The arithmetic operations above are automatically performed on each element of the series. By default, SPL series begin at index 1.

 

SPL arrays are organized in row-column format so that

 

data[3, 4]

 

refers to the element in the 3rd row of the 4th column of the array. Like series, array indices start at index 1.

 

SPL also supports C/C++ style array references:

 

data[3][4]

 

Indices themselves may be series. For example:

 

a = {10, 20, 30, 40};

b = a[{3, 2, 1}];

 

The series b contains the values {30, 20, 10}.

 

The { } creates series in a variety of ways:

 

 a = {1, 2, 3};

 

produces a three element series.

 

 a = {0, a, 0};

 

yields the series {0, 1, 2, 3, 0}

 

 b = {{1, 2}, {3, 4}, {5, 6}};

 

produces the 3x2 array:

 

 {{1, 2},

  {3, 4},

  {5, 6}}

 

The .. operator creates a linear increasing or decreasing series.

 

 start..increment..end

 

The increment is defaulted to 1 if not specified. For example,

 

 1..5

 

produces the series {1, 2, 3, 4, 5}

 

 5..-1..1

 

yields {5, 4, 3, 2, 1}

 

 1..0.8..5

 

returns {1, 1.8, 2.6, 3.4, 4.2, 5}

 

Since the .. operator creates a series and series can reference arrays, .. can access array elements.

 

a = 2..2..12;

b = a[2..6];

c = a[2..2..6];

d = a[..];

 

a == {2, 4, 6, 8, 10, 12}

b == {4, 6, 8, 10, 12}

c == {4, 8, 12}

d == {2, 4, 6, 8, 10, 12}

 

The .. operator by itself implies all rows or columns. Assigning an array using the .. operator preserves the shape of the array.

 

a = {{1, 4, 7}, 

     {2, 5, 8}, 

     {3, 6, 9}}; 

 

a[..] = 2..2..18; 

 

a == {{2, 8, 14}, 

      {4, 10, 16}, 

      {6, 12, 18}} 

 

SPL also supports linear or unraveled indexing. For example,

 

a = ravel(100..200, 10);

 

creates a 10x10 array of elements ranging from 100 through 200.

 

Now, a[11] == a[1, 2] == 110 since both a[11] and a[1, 2] refer to the 11th element of array a.

 

The END function returns the last array index or last array row or column index. For example:

 

a = 1..10;

b = a[end];

b == 10