[DGD] Array unpleasantness

Robert Forshaw iouswuoibev at hotmail.com
Sat Feb 21 04:05:08 CET 2004


When I have a situation where I wish to add another element to an array, I 
might do this:

arr += ({ new_element });

However, arrays are initialised to nil, and DGD doesn't know how to add an 
array to nil and so this would result in an error if the array isn't 
initiali\ed. One way around this would be to initialise it in the create() 
function.

create()
{
    arr = allocate(0);
    /* or */
    arr = ({ });
}

However, slight problem here: when adding an array containing one element to 
an array containing zero elements, the first element is left as a blank 
element! So:

array_of_objects += ({ another_object }) == ({ nil, another_object })

This means that the first element is left as nil, which is a waste if 
nothing else. My solution is as follows, when attempting to add another 
element to the array:

if(!array_of_objects || !sizeof(array_of_objects))
    array_of_objects = ({ another_object });
else
    array_of_objects += ({ another_object });


It works, too. But boy is it ugly. I don't feel I should have to type this 
just to achieve the effect I want. Is there a method I've overlooked, or is 
DGD lacking in this regard? Or is there a good reason why it works the way 
it does?...

_________________________________________________________________
Express yourself with cool emoticons - download MSN Messenger today! 
http://www.msn.co.uk/messenger

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list