Confusing stuff with arrays

Tom S h3r3tic at remove.mat.uni.torun.pl
Fri May 12 05:34:12 PDT 2006


Deewiant wrote:
> Of course I managed to forget the most important question for me: is there a
> reasonable way of doing what I'm trying to do?
> 
> I.e. is there a template or whatever which would allow me to properly initialise
> an array of type char[][2][] with, say, [["Foo", "bar"], ["Bar", "foo"]] inline?

Yeah, static arrays are quite nasty to work with, but maybe the 
following code will be enough for your needs ?


import std.stdio;


char[][] str2(char[] a, char[] b) {
     char[][] s;
     s ~= a;
     s ~= b;
     return s;
}


char[][2][] arrstr2(char[][][] items ...) {
     char[][2][] res;

     foreach (char[][] x; items) {
         assert (2 == x.length);

         char[][2] x2;
         x2[0] = x[0].dup;
         x2[1] = x[1].dup;
         res ~= x2;
     }

     return res;
}


void main() {
     char[][2][] myArr = arrstr2(str2("Foo", "bar"), str2("Bar", "foo"));

     foreach (char[][2] a; myArr) {
         writef("[ ");
         foreach (char[] x; a) {
             writef(x, ' ');
         }
         writefln(']');
     }
}



-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O 
!M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y
------END GEEK CODE BLOCK------

Tomasz Stachowiak  /+ a.k.a. h3r3tic +/



More information about the Digitalmars-d-learn mailing list