www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initializing associative arrays

reply Tydr Schnubbis <fake address.dude> writes:
If I've got this array:

char[][][char[]] arr;

and want to initialize it, what do I do?  I couldn't make the 
initializer syntax work, I tried this:

arr = [["key1"]: ["one", "two"]];

Then I resorted to this:

static this() {
     arr["key1"] = split("one two");
}

And that works, but doesn't strike me as particularly obvious or 
practical.  It's a lot like the string list initializer trick I often 
use in python code.

Have I overlooked something here? Or maybe static initializers just 
aren't the right way to go for hashes?
May 01 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Tydr Schnubbis" <fake address.dude> wrote in message 
news:e35k35$50a$1 digitaldaemon.com...
 Have I overlooked something here? Or maybe static initializers just aren't 
 the right way to go for hashes?
They simply don't exist in D. Nor do array initializers for any kind of non-static arrays. Except character arrays. They get special treatment.
May 01 2006
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Tydr Schnubbis wrote:
 If I've got this array:
 
 char[][][char[]] arr;
 
 and want to initialize it, what do I do?  I couldn't make the 
 initializer syntax work, I tried this:
 
 arr = [["key1"]: ["one", "two"]];
<snip> Problem 1: That isn't an initialiser. An initialiser is what follows the '=' in a _declaration_. Problem 2: You've declared the key type as char[]. But you're trying to use a char[][] as the key. Problem 3: D doesn't currently support initialisers for AAs. Don't ask me why - I guess Walter just hasn't got round to putting it in. But implementing the facility would at least enable this to work: char[][][char[]] arr = ["key1": ["one", "two"]]; Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
May 02 2006