Thread overview
assoc array initialization like in php
Feb 20, 2006
dennis luehring
Feb 20, 2006
Chris Sauls
Feb 20, 2006
nick
Feb 20, 2006
Derek Parnell
February 20, 2006
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


February 20, 2006
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;
February 20, 2006
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
February 20, 2006
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
February 20, 2006
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
February 20, 2006
> 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.
February 20, 2006
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