digitalmars.D.learn - Counting the number of elements in int[] ?
- AEon (11/11) Mar 21 2005 I use an int array:
- Derek Parnell (13/22) Mar 21 2005 In your source declare ...
- AEon (8/27) Mar 21 2005 I ended up doing this in my function:
- Derek Parnell (11/46) Mar 21 2005 My mistake. I thought that the 'cfg_Section_Found' was declared at the
- AEon (3/41) Mar 22 2005 Aha... amazing what D will let you do.
I use an int array: const int[] cfg_Quote_Counts = [ 2, 8, 6, 4, 0 ]; and would like to use the number of entries, to size another array: int[cfg_Quote_Counts.max] cfg_Section_Found; For some reason, neither .length nor .max will work, even though with enum, .max seems to be something like .length: enum Color { red, blue, green }; int value[Color.max] = [ blue:6, green:2, red:5 ]; This latter example is from the documentation. And ironically does not compile. Any idea? AEon
Mar 21 2005
On Mon, 21 Mar 2005 22:55:22 +0000 (UTC), AEon wrote:I use an int array: const int[] cfg_Quote_Counts = [ 2, 8, 6, 4, 0 ]; and would like to use the number of entries, to size another array: int[cfg_Quote_Counts.max] cfg_Section_Found; For some reason, neither .length nor .max will work, even though with enum, .max seems to be something like .length:In your source declare ... At run time use this instead ... -- Derek Melbourne, Australia 22/03/2005 10:08:13 AM
Mar 21 2005
In article <1lg2ca49o7zw4$.8d8snaa4rq07$.dlg 40tude.net>, Derek Parnell says...On Mon, 21 Mar 2005 22:55:22 +0000 (UTC), AEon wrote:I ended up doing this in my function: int[] cfg_Section_Found; cfg_Section_Found.length = cfg_Quote_Counts.length; cfg_Section_Found[] = 0; alas I am still unclear how your above "static this()" is used. Simply place it somewhere in a module, e.g. outside of a function? AEonI use an int array: const int[] cfg_Quote_Counts = [ 2, 8, 6, 4, 0 ]; and would like to use the number of entries, to size another array: int[cfg_Quote_Counts.max] cfg_Section_Found; For some reason, neither .length nor .max will work, even though with enum, .max seems to be something like .length:In your source declare ... At run time use this instead ...
Mar 21 2005
On Mon, 21 Mar 2005 23:54:45 +0000 (UTC), AEon wrote:In article <1lg2ca49o7zw4$.8d8snaa4rq07$.dlg 40tude.net>, Derek Parnell says...My mistake. I thought that the 'cfg_Section_Found' was declared at the module level and not inside a function. The "static this()" is used to run code before the main() function is called. It is typically used to initialize module level variables. The way you have now done it is fine for variables declared inside a function.On Mon, 21 Mar 2005 22:55:22 +0000 (UTC), AEon wrote:I ended up doing this in my function: int[] cfg_Section_Found; cfg_Section_Found.length = cfg_Quote_Counts.length; cfg_Section_Found[] = 0; alas I am still unclear how your above "static this()" is used.I use an int array: const int[] cfg_Quote_Counts = [ 2, 8, 6, 4, 0 ]; and would like to use the number of entries, to size another array: int[cfg_Quote_Counts.max] cfg_Section_Found; For some reason, neither .length nor .max will work, even though with enum, .max seems to be something like .length:In your source declare ... At run time use this instead ...Simply place it somewhere in a module, e.g. outside of a function?Yes. -- Derek Melbourne, Australia 22/03/2005 11:11:57 AM
Mar 21 2005
Derek Parnell says...Aha... amazing what D will let you do. AEonMy mistake. I thought that the 'cfg_Section_Found' was declared at the module level and not inside a function. The "static this()" is used to run code before the main() function is called. It is typically used to initialize module level variables. The way you have now done it is fine for variables declared inside a function.On Mon, 21 Mar 2005 22:55:22 +0000 (UTC), AEon wrote:I ended up doing this in my function: int[] cfg_Section_Found; cfg_Section_Found.length = cfg_Quote_Counts.length; cfg_Section_Found[] = 0; alas I am still unclear how your above "static this()" is used.I use an int array: const int[] cfg_Quote_Counts = [ 2, 8, 6, 4, 0 ]; and would like to use the number of entries, to size another array: int[cfg_Quote_Counts.max] cfg_Section_Found; For some reason, neither .length nor .max will work, even though with enum, .max seems to be something like .length:In your source declare ... At run time use this instead ...Simply place it somewhere in a module, e.g. outside of a function?
Mar 22 2005