www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Counting the number of elements in int[] ?

reply AEon <AEon_member pathlink.com> writes:
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
parent reply Derek Parnell <derek psych.ward> writes:
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
parent reply AEon <AEon_member pathlink.com> writes:
In article <1lg2ca49o7zw4$.8d8snaa4rq07$.dlg 40tude.net>, Derek Parnell says...
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 ...
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? AEon
Mar 21 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 21 Mar 2005 23:54:45 +0000 (UTC), AEon wrote:

 In article <1lg2ca49o7zw4$.8d8snaa4rq07$.dlg 40tude.net>, Derek Parnell says...
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 ...
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.
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.
 
 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
parent AEon <AEon_member pathlink.com> writes:
Derek Parnell says...

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 ...
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.
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.
 
 Simply place it somewhere in a module, e.g. outside of a function?
Aha... amazing what D will let you do. AEon
Mar 22 2005