www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - setting values of an array

reply clayasaurus <clayasaurus_member pathlink.com> writes:
hey, right now i'm using a function like this
to set a dynamic array's properties

int[] array; set3array(array,1,2,3);

void set3array(inout ALfloat[] array, float x, float y, float z)
{
array.length = 3;
array[0] = x;
array[1] = y;
array[2] = z;
}

is there a better way to set array values than this?
Jun 14 2004
parent reply Charlie <Charlie_member pathlink.com> writes:
array ~= x;
array ~= y;
array ~= z;

?

C

In article <cal65o$2e3b$1 digitaldaemon.com>, clayasaurus says...
hey, right now i'm using a function like this
to set a dynamic array's properties

int[] array; set3array(array,1,2,3);

void set3array(inout ALfloat[] array, float x, float y, float z)
{
array.length = 3;
array[0] = x;
array[1] = y;
array[2] = z;
}

is there a better way to set array values than this?
Jun 14 2004
parent reply clayasaurus <clayasaurus_member pathlink.com> writes:
it is just so much nicer to be able to set the contents of an array 
in one line. 

I like the C way: array[] = {1,2,3}; 

is there a good reason why D doesn't have this? 


In article <cal6us$2fab$1 digitaldaemon.com>, Charlie says...
array ~= x;
array ~= y;
array ~= z;

?

C

In article <cal65o$2e3b$1 digitaldaemon.com>, clayasaurus says...
hey, right now i'm using a function like this
to set a dynamic array's properties

int[] array; set3array(array,1,2,3);

void set3array(inout ALfloat[] array, float x, float y, float z)
{
array.length = 3;
array[0] = x;
array[1] = y;
array[2] = z;
}

is there a better way to set array values than this?
Jun 14 2004
next sibling parent Regan Heath <regan netwin.co.nz> writes:
On Mon, 14 Jun 2004 22:57:57 +0000 (UTC), clayasaurus 
<clayasaurus_member pathlink.com> wrote:
 it is just so much nicer to be able to set the contents of an array
 in one line.

 I like the C way: array[] = {1,2,3};

 is there a good reason why D doesn't have this?
No idea.. it works for char[] eg. char[] foo = "regan was here"; foo = "bob was here"; probably due to "regan was here" being converted to a char[] implicitly. This also works... ubyte[] b; b = cast(ubyte[])x"010203"; (sets b[0] to 1, b[1] to 2, and b[2] to 3) but is probably dependant on the endian-ness of the system it is compiled on? I tried to get one like the above working for float, but could not seem to. Regan
 In article <cal6us$2fab$1 digitaldaemon.com>, Charlie says...
 array ~= x;
 array ~= y;
 array ~= z;

 ?

 C

 In article <cal65o$2e3b$1 digitaldaemon.com>, clayasaurus says...
 hey, right now i'm using a function like this
 to set a dynamic array's properties

 int[] array; set3array(array,1,2,3);

 void set3array(inout ALfloat[] array, float x, float y, float z)
 {
 array.length = 3;
 array[0] = x;
 array[1] = y;
 array[2] = z;
 }

 is there a better way to set array values than this?
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 14 2004
prev sibling next sibling parent Regan Heath <regan netwin.co.nz> writes:
On Mon, 14 Jun 2004 22:57:57 +0000 (UTC), clayasaurus 
<clayasaurus_member pathlink.com> wrote:
 it is just so much nicer to be able to set the contents of an array
 in one line.

 I like the C way: array[] = {1,2,3};

 is there a good reason why D doesn't have this?
To answer this... Yes and No, it's because it's tricky and Walter has not yet implemented it. I believe this is called an "Array literal", your example above is un-ambiguous, but, there are several which are. See the other posts in this NG about array literals. Regan
 In article <cal6us$2fab$1 digitaldaemon.com>, Charlie says...
 array ~= x;
 array ~= y;
 array ~= z;

 ?

 C

 In article <cal65o$2e3b$1 digitaldaemon.com>, clayasaurus says...
 hey, right now i'm using a function like this
 to set a dynamic array's properties

 int[] array; set3array(array,1,2,3);

 void set3array(inout ALfloat[] array, float x, float y, float z)
 {
 array.length = 3;
 array[0] = x;
 array[1] = y;
 array[2] = z;
 }

 is there a better way to set array values than this?
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 14 2004
prev sibling next sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"clayasaurus" <clayasaurus_member pathlink.com> wrote in message
news:calahl$2l33$1 digitaldaemon.com...
 it is just so much nicer to be able to set the contents of an array
 in one line.

 I like the C way: array[] = {1,2,3};

 is there a good reason why D doesn't have this?
Hopefully D will have this one day.
 In article <cal6us$2fab$1 digitaldaemon.com>, Charlie says...
array ~= x;
array ~= y;
array ~= z;

?

C

In article <cal65o$2e3b$1 digitaldaemon.com>, clayasaurus says...
hey, right now i'm using a function like this
to set a dynamic array's properties

int[] array; set3array(array,1,2,3);

void set3array(inout ALfloat[] array, float x, float y, float z)
{
array.length = 3;
array[0] = x;
array[1] = y;
array[2] = z;
}

is there a better way to set array values than this?
Jun 14 2004
prev sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
clayasaurus wrote:

it is just so much nicer to be able to set the contents of an array 
in one line. 

I like the C way: array[] = {1,2,3}; 

is there a good reason why D doesn't have this? 
  
Although I haven't heard anything from Walter, I think this is just one of those things on his todo list. The D form would be: int array[] = [1,2,3]; Note that you can already do: const int array [] = [1,4,5]; -- -Anderson: http://badmama.com.au/~anderson/
Jun 15 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
J Anderson wrote:

<snip>
 int array[] = [1,2,3];
 Note that you can already do:
 
 const int array [] = [1,4,5];
Or static int array[] = [1, 4, 5]; But yes, as said before, it ought to work generally. http://www.digitalmars.com/drn-bin/wwwnews?D/26695 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.
Jun 15 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:camj35$1hod$1 digitaldaemon.com...
 J Anderson wrote:

 <snip>
 int array[] = [1,2,3];
 Note that you can already do:

 const int array [] = [1,4,5];
Or static int array[] = [1, 4, 5]; But yes, as said before, it ought to work generally.
But this isn't good when you don't want a const array. I often wanted to write int[] array = [1,2,x2-x1,y2-y1]; Especially useful it would be for multidimensional arrays.
 http://www.digitalmars.com/drn-bin/wwwnews?D/26695

 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.
Jun 15 2004
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Ivan Senji wrote:
 "Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
 news:camj35$1hod$1 digitaldaemon.com...
 
<snip>
 But yes, as said before, it ought to work generally.
But this isn't good when you don't want a const array. I often wanted to write int[] array = [1,2,x2-x1,y2-y1];
<snip> I suppose "generally" would include this just as well. 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.
Jun 15 2004