Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to Slice a dynamic array?

250 views
Skip to first unread message

Bob Rasmusens

unread,
May 16, 2003, 5:02:53 AM5/16/03
to
Is there a way to Slice dynamic Arrays? I am looking to do something
like the following.

Data : array of double;

SetLength(Data,1000);

MyProcedure(Slice(Data[5],100));

Ignacio Vazquez

unread,
May 15, 2003, 5:03:29 PM5/15/03
to
"Bob Rasmusens" <wjg...@weir-jones.com> wrote in message
news:3EC4A93D...@weir-jones.com...

What's not working with the existing Slice function?

Boris Novgorodov

unread,
May 15, 2003, 11:11:21 PM5/15/03
to
Slice selects the first Count elements of the array!
You can use Move to copy range of elements to new array or make procedure
like this:

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

Avatar Zondertau

unread,
May 16, 2003, 5:26:53 AM5/16/03
to
> MyProcedure(Slice(Data[5],100));

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.

Bob Rasmusens

unread,
May 17, 2003, 12:15:12 AM5/17/03
to
If you use any of the following
MyProcedure(Slice(Data,100))
MyProcedure(Slice(Data[5],100))
you get the error "Array type required".

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.

Ignacio Vazquez

unread,
May 16, 2003, 12:13:53 PM5/16/03
to
"Bob Rasmusens" <wjg...@weir-jones.com> wrote in message
news:3EC5B74F...@weir-jones.com...

> If you use any of the following
> MyProcedure(Slice(Data,100))
> MyProcedure(Slice(Data[5],100))
> you get the error "Array type required".
>
> 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."

How is MyProcedure declared?

Cheers,
Ignacio

Rudy Velthuis (TeamB)

unread,
May 16, 2003, 1:33:19 PM5/16/03
to
In <3ec50f57$1...@newsgroups.borland.com>, Ignacio Vazquez wrote:

> > 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

Rudy Velthuis (TeamB)

unread,
May 16, 2003, 1:31:40 PM5/16/03
to

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.

Anders Isaksson

unread,
May 19, 2003, 6:16:25 AM5/19/03
to
On 16 May 2003 10:31:40 -0700, "Rudy Velthuis (TeamB)"
<rvel...@gmx.de> wrote:

>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

Rudy Velthuis (TeamB)

unread,
May 19, 2003, 8:05:22 AM5/19/03
to
In <39bhcvkjjoci46q2i...@4ax.com>, Anders Isaksson wrote:

> 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.

Anders Isaksson

unread,
May 19, 2003, 10:35:50 AM5/19/03
to
On 19 May 2003 05:05:22 -0700, "Rudy Velthuis (TeamB)"
<rvel...@gmx.de> wrote:

>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?

0 new messages