[fpc-pascal] Question about Deleting elements in Dynamic Array

Henry Vermaak henry.vermaak at gmail.com
Tue Apr 27 17:19:45 CEST 2010


On 27 April 2010 14:42, Bihar Anwar <bihar_anwar at rocketmail.com> wrote:
> Just say, I have a dynamic array with size=5, and I want to delete elements from the index 0 to 2. Is there a trick (the fastest way) to delete those elements (0 to 2) without moving activities?
>
> I've tried to make the dynamic array just pointing to 3rd element and set a new length for it, but fpc generates an unhandled exception.
>
> var
>  a: array of string;
>
> SetLength(a, 5);
> a[0] := 'aa'; a[1] := 'bb'; a[2] := 'cc'; a[3] := 'dd'; a[4] := 'ee';
>
> a := @sl[3];       // this is the trick, but it doesn't work.
> SetLength(a, 2)

I think the right way to do this is:

a := copy(a, 3, length(a) - 3);

Presumably copy optimizes this adequately.

Henry



More information about the fpc-pascal mailing list