Thread overview
Removing Elements from an Array
Mar 26, 2004
resistor
Mar 26, 2004
Matthew
Mar 26, 2004
Stewart Gordon
Mar 26, 2004
Russ Lewis
March 26, 2004
The associative array documentation says I can write something like:

delete it["foo"];

Can I do that with a normal array as well? Would:

delete it[3];

erase that object, or remove it from the array?

Owen


March 26, 2004
Not AFAIK

<resistor@mac.com> wrote in message news:c40b0j$g38$1@digitaldaemon.com...
> The associative array documentation says I can write something like:
>
> delete it["foo"];
>
> Can I do that with a normal array as well? Would:
>
> delete it[3];
>
> erase that object, or remove it from the array?
>
> Owen
>
>


March 26, 2004
resistor@mac.com wrote:

> The associative array documentation says I can write something like:
> 
> delete it["foo"];

I guess that's another wart - I'm not sure what was wrong with

	it.remove("foo");

or similar.  But anyway....

> Can I do that with a normal array as well? Would:
> 
> delete it[3];
> 
> erase that object, or remove it from the array?

Erase the object, I imagine.  If it removed it, what would it put in its place?

Stewart.

-- 
My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment.  Please keep replies on the 'group where everyone may benefit.
March 26, 2004
resistor@mac.com wrote:
> The associative array documentation says I can write something like:
> 
> delete it["foo"];
> 
> Can I do that with a normal array as well? Would:
> 
> delete it[3];
> 
> erase that object, or remove it from the array?

D doesn't have many warts, but this is one of them.  'delete' on an associative array deletes the object from the array but LEAVES THE OBJECT ALONE.  So, the object is still valid.  But 'delete' on a normal array deletes the OBJECT POINTED TO but LEAVES THE REFERENCE INTACT.

Unfortunately, this can make code very confusing:
	Object[] foo1;
	Object[int] foo2;
	...
	delete foo1[3];
	delete foo2[3];

The first 'delete' deletes an Object variable, but leaves a reference to the (now invalid) Object in the array.

The second 'delete' deletes the Object from the array, but leaves the object intact.  So if you have other references to the Object somewhere, those are still valid.  Of course, if this is the last reference to the Object, then the garbage collector will clean it up eventually.