[DGD] Re: Array unpleasantness

Erwin Harte harte at is-here.com
Sat Feb 21 04:15:33 CET 2004


On Sat, Feb 21, 2004 at 03:05:08AM +0000, Robert Forshaw wrote:
> 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 })

I don't recall ever seeing that.

To verify:

  > @code allocate(0)
  $8 = ({ })
  > @code $8 + ({ "foo" })
  $9 = ({ "foo" })

I feel we're not seeing the complete picture because what you describe
is not behaviour of DGD as I know it.

An empty array + any other array ends up being a copy of the right
hand side (it's not the same pointer but the element point to the same
values).  So using arr += ({ new_element }) should work given the
initialization that you suggested.

Hope that helps,

Erwin.
-- 
Erwin Harte <harte at is-here.com>
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list