Data : array of double;
SetLength(Data,1000);
MyProcedure(Slice(Data[5],100));
What's not working with the existing Slice function?
procedure MyProc(var a; Count:Integer);
type TDblArr=array[word] of double;
PDblArr=^TDblArr;
var DA:PDblArr;
i:Integer;
begin
DA:=@a;
for i:=0 to Count-1 do
... use DA^[i] here ...
--
Regards
Boris Novgorodov
Data := Copy(Data, 5, 100);
The standard Sytem unit Copy function works for arrays just like it does for
strings. This is probably the easiest way to take a part of an array.
From what I can see you can not slice a Dynamic Array.
I have even tried various combinations of the following
MyProcedure(Slice(PStaticDoubleArray(@Data[0])^,Count));
which does not give any errors but also does not pass any data to the
procedure.
The Copy function involves duplication of data which I do not want to do.
From the Delphi help file (Slice function):
"Slice is only allowed as a parameter in a call to a procedure or function
that expects an open array parameter."
How is MyProcedure declared?
Cheers,
Ignacio
> > From what I can see you can not slice a Dynamic Array.
>
> From the Delphi help file (Slice function):
>
> "Slice is only allowed as a parameter in a call to a procedure or
> function that expects an open array parameter."
True, but you get a different error message if that is not the case. He's
correct. Slice doesn't seem to work on dynarrays.
--
Rudy Velthuis (TeamB)
"Ask her to wait a moment - I am almost done."
-- Carl Friedrich Gauss (1777-1855), while working, when informed that
his wife is dying
Data[5] is not an array type, it is a Double.
But the real problem is, that Slice only seems to work on static array
types.
>In <3EC4A93D...@weir-jones.com>, Bob Rasmusens wrote:
>
>> Is there a way to Slice dynamic Arrays? [...]
>
>But the real problem is, that Slice only seems to work on static array
>types.
But you can send both a static array, and a *full* dynamic array to the
same procedure, and do the Slice-ing there. See the following extract:
var
ar: array[0..10] of Integer; // static
dar: array of Integer; // dynamic
i: Integer;
// Initialize the arrays to something...
procedure TForm1.FormCreate(Sender: TObject);
begin
SetLength(dar, 5);
for i := 0 to 10 do begin
ar[i] := i;
if i < 5 then
dar[i] := 100*i;
end;
end;
procedure TForm1.ShowAll(a: array of integer);
var i: integer;
begin
Listbox1.Items.Clear;
for i := Low(a) to High(a) do begin
ListBox1.Items.Add(IntToStr(a[i]));
end;
end;
// This one accepts both static and dynamic arrays
procedure TForm1.MyShowSlice(a: array of integer; c: Integer);
begin
ShowSlice(Slice(a, c));
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ShowAll(ar);
MyShowSlice(ar, 4);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ShowAll(dar);
MyShowSlice(dar, 4);
end;
--
Anders Isaksson, Sweden
BlockCAD: http://user.tninet.se/~hbh828t/proglego.htm
Gallery: http://user.tninet.se/~hbh828t/gallery/index.htm
> But you can send both a static array, and a full dynamic array to the
> same procedure, and do the Slice-ing there. See the following extract:
> // This one accepts both static and dynamic arrays
>
> procedure TForm1.MyShowSlice(a: array of integer; c: Integer);
> begin
> ShowSlice(Slice(a, c));
> end;
Yes, but that doesn't take away that Slice doesn't accept dynamic arrays
types, for reasons unknown to me. The Slice *inside* the procedure
accepts open arrays, and of course these can be dynamic as well. But it
is not the same. It is a workaround for something that is IMO not
necessary.
>The Slice *inside* the procedure accepts open arrays, and of course these
>can be dynamic as well. But it is not the same. It is a workaround for
>something that is IMO not necessary.
Of course it's ridiculous that Slice() doesn't take dynamic arrays as
argument. It's an unnecessary restriction in the compiler.
I never said Delphi was right, I showed a way to slice both static and
dynamic arrays, which was the original question, right?