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

Delete an entry in a dynamic array of records

2,313 views
Skip to first unread message

Jeff Guillaume

unread,
Jul 18, 2001, 3:21:09 PM7/18/01
to
the setup:
...
type
TSourceRec = record
ID: Integer;
Name: String[50];
LocalFTPPath: String[100];
NetworkFTPPath: String[100];
NotifyAddress: String[100];
NotifySuccess: Boolean;
NotifyFailure: Boolean;
DTSPackage: String[100];
end;

var
SourceArray: Array of TSourceRec;
...

I can't figure out how to remove a specific entry at index "i". For example,
SourceArray.Delete(i)/RemoveItem(i) or something of the sort. I just want to
delete the entry at position "i" in the array. Is it possible?!

Thanks much in advance!

Jeff Guillaume
ColdFusion Developer
Direct Dial: (877) 399-9108
Direct Fax: (419) 793-2382
StoneAge.com: (800) STONEAGE x 280
E-mail: jguil...@stoneage.com


John Herbster

unread,
Jul 18, 2001, 3:33:49 PM7/18/01
to
Jeff Guillaume <jeffgu...@hotmail.com> wrote
> type
> TSourceRec = record
> ...

> end;
>
> var
> SourceArray: Array of TSourceRec;
> ...
>
> I can't figure out how to remove a specific entry at index "i".
For example,
> SourceArray.Delete(i)/RemoveItem(i) or something of the sort. I
just want to
> delete the entry at position "i" in the array. Is it possible?!

Jeff, Have you tried the obvious. Regards, JohnH

procedure Delete(var SourceArray: Array of TSourceRec; i: integer);
var j: integer;
begin
if (i<0) or (i>=length(SourceArray))
then EXIT;
For j := i to length(SourceArray)-2 do
SourceArray[j] := SourceArray[j+1];
SetLength(SourceArray,length(SourceArray)-1);
end;

UNTESTED!

Jeff Guillaume

unread,
Jul 18, 2001, 3:40:37 PM7/18/01
to
Well, I was hoping for something more elegant and non-type specific (though
it doesn't really need to be). I just looked into TList, and it seems to
have a lot of what I want, but what you suggested should work too.

I just can't decide whether to switch to TList now, or just hack my way
around for solutions as they come up. Suggestions? :)


"John Herbster" <jo...@petronworld.com> wrote in message
news:3b55e49d$1_2@dnews...

John Herbster

unread,
Jul 18, 2001, 3:50:32 PM7/18/01
to
Jeff wrote

>> I just can't decide whether to switch to TList now, or just hack
my way around for solutions as they come up. Suggestions? :)

Jeff, Here are a few differences:

A TList and its elements require Create and Free. TL's are good for
insertions and deletions as long as list is not too large. TList
requires type casting of the element pointers.

Dynamic arrays require just SetLength() and no cleanup. DA's are
even slower than TL's for insertions and deletions. For plain
access, DA's require no type casting.

And then there are also queues and hash tables.

Regards, JohnH


Peter Below (TeamB)

unread,
Jul 18, 2001, 7:03:01 PM7/18/01
to
In article <3b55e1a4$1_1@dnews>, Jeff Guillaume wrote:
> TSourceRec = record

> end;
>
> var
> SourceArray: Array of TSourceRec;
> ....

>
> I can't figure out how to remove a specific entry at index "i".


Delete( SourceArray, i, 1 );

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

John McTaggart

unread,
Jul 18, 2001, 7:17:00 PM7/18/01
to
On Wed, 18 Jul 2001 14:50:32 -0500, "John Herbster"
<jo...@petronworld.com> wrote:

>Jeff wrote
>>> I just can't decide whether to switch to TList now, or just hack
>my way around for solutions as they come up. Suggestions? :)

>A TList and its elements require Create and Free. TL's are good for


>insertions and deletions as long as list is not too large. TList
>requires type casting of the element pointers.
>
>Dynamic arrays require just SetLength() and no cleanup. DA's are
>even slower than TL's for insertions and deletions. For plain
>access, DA's require no type casting.

Actually, TLists are a static array of pointers which is probably
why they are a little quicker than dynamic arrays.

TPointerList = array[0..MaxListSize-1] of Pointer;

Another thing he could do to handle deletions is to add a delete
flag. Simply flag the item for deletion and cleanup when needed.
If I remember correctly, this is how DBase used to handle the
problem. If it saw a deletion flag, it skipped the entry and packed
the records on exit..

John McTaggart

John Herbster

unread,
Jul 18, 2001, 9:17:59 PM7/18/01
to
"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote

> In article <3b55e1a4$1_1@dnews>, Jeff Guillaume wrote:
> > TSourceRec = record
> > end;
> > var
> > SourceArray: Array of TSourceRec;
> > I can't figure out how to remove a specific entry at index "i".
>
> Delete( SourceArray, i, 1 );

Peter, Is this a D6 thing? Regards, JohnH


Rob Kennedy

unread,
Jul 19, 2001, 3:03:09 AM7/19/01
to
"Jeff Guillaume" <jeffgu...@hotmail.com> wrote in message
news:3b55e1a4$1_1@dnews...

> var
> SourceArray: Array of TSourceRec;
> ...
>
> I can't figure out how to remove a specific entry at index "i". For
example,
> SourceArray.Delete(i)/RemoveItem(i) or something of the sort. I just want
to
> delete the entry at position "i" in the array. Is it possible?!

You sent this same question to a mailing list, and I answered it for you
this afternoon--check your e-mail.

--Rob


Peter Below (TeamB)

unread,
Jul 19, 2001, 2:11:57 PM7/19/01
to
In article <3b563622$1_2@dnews>, John Herbster wrote:
> > Delete( SourceArray, i, 1 );
>
> Peter, Is this a D6 thing? Regards, JohnH
>

May be that D6 will allow that, but my post was more the result of
doing newsgroup work when i should have been asleep <g>. Doesn't work
in <= D5, of course. Sorry for the confusion.

John Herbster

unread,
Jul 19, 2001, 2:45:06 PM7/19/01
to
Peter Below (TeamB) <10011...@compuXXserve.com> wrote
> ... my post was more the result of doing newsgroup work when i

should have been asleep <g>.

Peter, You have pulled so many useful rabbits out of the hat that I
thought that this was just another one -- till I tried it. Best
regards and don't wear yourself out. JohnH


Kevin

unread,
Jul 20, 2001, 4:06:32 AM7/20/01
to

Jeff Guillaume <jeffgu...@hotmail.com> schrieb in im Newsbeitrag:
3b55e635_2@dnews...

> I just can't decide whether to switch to TList now, or just hack my way
> around for solutions as they come up. Suggestions? :)

I am a little biased, as I have never been too thrilled with dynamic arrays
since I often need to moving large chunks of data quickly. IMHO, the
dynamic array idea in its current form is good for infrequently changed
numeric or simple records, but it still lacks manipulative power to make it
a serious contender for what I need to do (too bad the overload procedure
for string like Delete does not exist, as this would have been really cool.
Delphi 7?).

Given that, with the complicated record structure you have I would probably
go with a TList if you might think that any time in the future you will want
to delete, insert, sort, or even pack records and you expect to have >
10,000 records. Anything below that number, then just the "obvious" option
already mentioned is just fine. If you use TList, you will have to use
New() and Dispose() your record pointers as you add and delete them (and
when the list if freed). And for more to think about, you can also change
your record to an object and use TObjectList as this can handle memory
management for you.

Finally, if you really want to get your hand dirty, you can create your own
dynamic array type for your record structure. That is somewhat more
complicated but very useful for fast memory management.

Cheers,

Kevin


0 new messages