www.digitalmars.com         C & C++   DMDScript  

D - Removing Elements from an Array

reply resistor mac.com writes:
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
Mar 25 2004
next sibling parent "Matthew" <matthew stlsoft.org> writes:
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

Mar 26 2004
prev sibling next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Mar 26 2004
prev sibling parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
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.
Mar 26 2004