www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - assoc array initialization like in php

reply dennis luehring <dennis_member pathlink.com> writes:
in php you can init an assoc array
(for example with string-key and numeric value)
with

$test = array
(
'test' => 1,
'bla' => 2,
'blub' => 3
);

can we have something equal to this in d?

int test[char[]] = 
{
"test" => 1,
"bla" => 2,
"blub" => 3
}

or an enum example

enum ekey { TEST, BLA, BLUB };
int test[ekey] =
{
TEST => 1,
BLA => 2,
BLUB => 3
}

or is this an working feature in d (i cant find such examples in the docs)

ciao dennis
Feb 20 2006
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
dennis luehring wrote:

 in php you can init an assoc array

 can we have something equal to this in d?

It's on the D wishlist, along with array inits... i.e. int test[] = [ 1,2,3 ]; See http://www.digitalmars.com/d/archives/26695.html Choosing a syntax would be a good thing, though ? maybe: int test[char[]] = [ "test": 1, "bla": 2, "blub": 3 ]; --anders PS. For now, you need to use explicit initializers instead: int test[char[]]; test["test"] = 1; test["bla"] = 2; test["blub"] = 3;
Feb 20 2006
next sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
Anders F Björklund wrote:
 Choosing a syntax would be a good thing, though ?
 
 maybe:
   int test[char[]] = [ "test": 1, "bla": 2, "blub": 3 ];

I vote for this one. It is consistant with struct initializers, and just pleases me eyes. -- Chris Nicholson-Sauls
Feb 20 2006
prev sibling next sibling parent Dawid =?UTF-8?B?Q2nEmcW8YXJraWV3aWN6?= <dawid.ciezarkiewicz gmail.com> writes:
Anders F Björklund wrote:
 Choosing a syntax would be a good thing, though ?
 
 maybe:
    int test[char[]] = [ "test": 1, "bla": 2, "blub": 3 ];

+1 looks good
Feb 20 2006
prev sibling parent reply nick <nick.atamas gmail.com> writes:
 PS.
 For now, you need to use explicit initializers instead:
   int test[char[]];
   test["test"] = 1;
   test["bla"] = 2;
   test["blub"] = 3;

You know, this syntax looks almost as good as the proposed ones. Perhaps no change in necessary.
Feb 20 2006
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
nick wrote:

For now, you need to use explicit initializers instead:
  int test[char[]];
  test["test"] = 1;
  test["bla"] = 2;
  test["blub"] = 3;

You know, this syntax looks almost as good as the proposed ones. Perhaps no change in necessary.

Just like in Java, it gets a little uglier when it's a "global": int test[char[]]; static this() { test["test"] = 1; test["bla"] = 2; test["blub"] = 3; } But having array/map literals, doesn't change this "old" syntax ? --anders
Feb 20 2006
prev sibling parent "Derek Parnell" <derek psych.ward> writes:
On Mon, 20 Feb 2006 21:35:54 +1100, dennis luehring  
<dennis_member pathlink.com> wrote:

 in php you can init an assoc array
 (for example with string-key and numeric value)
 with

 $test = array
 (
 'test' => 1,
 'bla' => 2,
 'blub' => 3
 );

You play around with the template syswtem to get something useful ... // --------------------- import std.c.stdarg; template setter(Tk, Td) { Td[Tk] setter ( ... ) { Td[Tk] lTemp; Tk key; Td data; int ready = 0; for (int i = 0; i < _arguments.length; i+=2) { key = va_arg!(Tk)(_argptr); data = va_arg!(Td)(_argptr); lTemp[ key ] = data; } return lTemp; } } import std.stdio; void main() { auto test = setter!(char[], int)( "test", 1, "bla", 2, "blub", 3 ); foreach(char[] x, int q; test) writefln("Key=%s, Data = %s", x, q); } // --------------------- -- Derek Parnell Melbourne, Australia
Feb 20 2006