Thread overview
Counting the number of elements in int[] ?
Mar 21, 2005
AEon
Mar 21, 2005
Derek Parnell
Mar 21, 2005
AEon
Mar 22, 2005
Derek Parnell
Mar 22, 2005
AEon
March 21, 2005
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
March 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 ...
#    int[] cfg_Section_Found;

At run time use this instead ...

# // Module constructor.
# static this()
# {
#    cfg_Section_Found.length = cfg_Quote_Counts.length;
# }


-- 
Derek
Melbourne, Australia
22/03/2005 10:08:13 AM
March 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 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 ...
>#    int[] cfg_Section_Found;
>
>At run time use this instead ...
>
># // Module constructor.
># static this()
># {
>#    cfg_Section_Found.length = cfg_Quote_Counts.length;
># }

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
March 22, 2005
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 ...
>>#    int[] cfg_Section_Found;
>>
>>At run time use this instead ...
>>
>># // Module constructor.
>># static this()
>># {
>>#    cfg_Section_Found.length = cfg_Quote_Counts.length;
>># }
> 
> 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
March 22, 2005
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 ...
>>>#    int[] cfg_Section_Found;
>>>
>>>At run time use this instead ...
>>>
>>># // Module constructor.
>>># static this()
>>># {
>>>#    cfg_Section_Found.length = cfg_Quote_Counts.length;
>>># }
>> 
>> 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