Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 14, 2004 setting values of an array | ||||
---|---|---|---|---|
| ||||
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? |
June 14, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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? > > > |
June 14, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charlie | 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? >> >> >> > > |
June 14, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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/ |
June 15, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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/ |
June 15, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | "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? > >> > >> > >> > > > > > > > > |
June 15, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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/ |
June 15, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | 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. |
June 15, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | "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. |
June 15, 2004 Re: setting values of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | 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. |
Copyright © 1999-2021 by the D Language Foundation