Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice a dynamic array?

How can I slice a dynamic array to multiple sub arrays? Slice() function in Delphi does not support dynamic arrays. So how can it be done? A generic solution would be welcome.

program Project10;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

var
  A: Array of Integer;
begin
  SetLength(A, 4);
  Slice(A, 2); // [dcc32 Error] Project10.dpr(15): E2193 Slice standard function only allowed as open array argument
end.
like image 234
user3764855 Avatar asked Mar 20 '23 05:03

user3764855


1 Answers

Use Copy(A, 0, 2) instead of Slice(A, 2).

The point is that either you need an "open array parameter" (in which case you need Slice) or you need a regular array, in which case Copy will provide a good solution.

like image 165
Daniel Avatar answered Mar 28 '23 06:03

Daniel