www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative Arrays - Initialize via [ value, value, ...] ?

reply AEon <aeon2001 lycos.de> writes:
Is there some way to initialize Associative Arrays via "lists"?


For a static array:

static char[][] month	= [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

will work fine, but for

static int[char[]]	Month = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

it will not...

Is there some way to avoid having to do a "manual" feed via:

static int[char[]] Month;
Month["Jan"] = 0;
Month["Feb"] = 1;
etc.

in a foreach loop?

AEon
Mar 27 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Sun, 27 Mar 2005 18:54:07 +0200, AEon <aeon2001 lycos.de> wrote:
 Is there some way to initialize Associative Arrays via "lists"?


 For a static array:

 static char[][] month	= [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",  
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

 will work fine, but for

 static int[char[]]	Month = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",  
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

 it will not...

 Is there some way to avoid having to do a "manual" feed via:

 static int[char[]] Month;
 Month["Jan"] = 0;
 Month["Feb"] = 1;
 etc.

 in a foreach loop?

Not yet, but hopefully one day we will be able to say something like: static int[char[]] Month = [ "Jan", "Feb", "Mar" ..etc.. //meaning an associative array with the defined keys, and default values static int[char[]] Month = [ "Jan":1, "Feb":2, "Mar":3, ..etc.. //meaning an associative array with the defined keys, and defined values or even a mixture of the two. Regan
Mar 27 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Regan Heath wrote:
<snip>
 Not yet, but hopefully one day we will be able to say something like:
 
 static int[char[]] Month = [ "Jan", "Feb", "Mar" ..etc..
 //meaning an associative array with the defined keys, and default values

Why not the defined values and default keys, which would make it consistent with static initalisers on linear arrays? Moreover, by "default values" do you mean the default initialiser for the value type, or what? Whichever way, it seems a crocky syntax, and I'd go for requiring AA initialisers to use explicit key:value pairs. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on on the 'group where everyone may benefit.
Apr 03 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Sun, 03 Apr 2005 17:46:22 +0100, Stewart Gordon <smjg_1998 yahoo.com>  
wrote:
 Regan Heath wrote:
 <snip>
 Not yet, but hopefully one day we will be able to say something like:
  static int[char[]] Month = [ "Jan", "Feb", "Mar" ..etc..
 //meaning an associative array with the defined keys, and default values

Why not the defined values and default keys, which would make it consistent with static initalisers on linear arrays?

I'm not sure either makes any sense.. I was just throwing the idea out there.
 Moreover, by "default values" do you mean the default initialiser for  
 the value type, or what?

Yep.
 Whichever way, it seems a crocky syntax

Agreed.
 , and I'd go for requiring AA initialisers to use explicit key:value  
 pairs.

Yeah. Probably the only way that makes any sense. Regan
Apr 03 2005