assoc array initialization like in php

Derek Parnell derek at psych.ward
Mon Feb 20 06:06:08 PST 2006


On Mon, 20 Feb 2006 21:35:54 +1100, dennis luehring  
<dennis_member at 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



More information about the Digitalmars-d mailing list