"delete" of entire tables/sets

I'm working on some scripts where I want to remove every element from a
table in a single shot. In awk, "delete tbl" would do the trick, but Zeek
restricts delete operations to removing single elements. Worse, if I try
iterating over an aggregate to remove elements piece-wise, it doesn't
remove all of them, no doubt because the internal hash table is changing
mid-stream of the "for" loop and thus doesn't visit all of the members.

This means I have to first build up a *separate* vector of all the indexes,
then iterate over that to remove them.

Proposal: extend "delete" so that if applied to a table or a set, it clears
out all of its elements.

This seems straightforward to me, and something that I'll see if I can
find some cycles soon to hack out.

Comments?

    Vern

This means I have to first build up a *separate* vector of all the indexes,
then iterate over that to remove them.

I guess re-assigning a new, empty table to the variable could be
analogous to deleting all entries and also avoids the iterator
invalidation problem ?

Proposal: extend "delete" so that if applied to a table or a set, it clears
out all of its elements.

But yeah, that also seems clearer to me at first thought.

- Jon

I guess re-assigning a new, empty table to the variable could be
analogous to deleting all entries and also avoids the iterator
invalidation problem ?

Hah!, yeah, that's certainly a simple way to do it. Maybe I'll
change my hacking for now to just be adding this observation to
the "delete" documentation :-).

    Vern

I believe the clear_table() function still exists in zeek, as well…

I believe the clear_table() function still exists in zeek, as well...

Hah^2! Yeah, it does. Well, glad I consulted the list before diving into
some hacking :-P.

    Vern

To satisfy my curiosity and to provide further info for anyone viewing list archives, I benchmarked assigning a new table() vs. running clear_table() on tables of various sizes & complexities. Assigning a new table() was clearly the winner by a huge margin.

It should be noted that these two methods are not completely equivalent. I run a script where a current table is periodically moved to an old table, and the current table cleared for further use. So, with A & B being tables of the same type:

B = A;
A = table();

works as intended, as B now contains the prior contents of A, and A is a new empty table.

B = A;
clear_table(A);

clears both, as they both point to the same table.