SmartPascal
Slice
Function
Creates a slice of an array as an Open Array parameter System unit
 function Slice ( SourceArray : array; Count : Integer ) : array;
Description
The Slice function creates a sub-array from the first Count elements of an array, SourceArray.
 
It can ONLY be used as an argument for an 'Open array' parameter of a procedure or function.
 
An 'Open array' parameter is one that has an unknown number of array elements at compile time.
 
This allows for a routine to work with variable sized arrays.
Related commands
Array A data type holding indexable collections of data
Copy Create a copy of part of a string or an array
 
Example code : Passing an array slice to a procedure
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure ShowSlice(SubArray : array of Integer);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  i : Integer;
  Source : array[0..4] of Integer;

begin
  // Create the source array with 0..4 values for elements 0..4
  for i := 0 to 4 do
    Source[i] := i;

  // Use the Slice command to pass just the first 3 elements of Source as
  // an open array to the ShowSlice procedure below.
  ShowSlice(Slice(Source, 3));
end;

// Show an array of unknown size - it is passed as an 'Open' array
procedure TForm1.ShowSlice(SubArray : array of Integer);
var
  i : Integer;

begin
  // Show every element of this array
  for i := 0 to Length(SubArray)-1 do
    ShowMessage('SubArray['+IntToStr(i)+'] : '+ IntToStr(SubArray[i]));
end;
 
end.
Hide full unit code
   SubArray[0] : 0
   SubArray[1] : 1
   SubArray[2] : 2