Regarding writefln formatting

Kenji Hara k.hara.pg at gmail.com
Thu Mar 22 02:56:48 PDT 2012


On Wednesday, 21 March 2012 at 01:26:23 UTC, bearophile wrote:
> import std.stdio;
> void main() {
>      auto mat = [[1, 2, 3],
>                  [4, 5, 6],
>                  [7, 8, 9]];
>
>      writefln("%(%(%d %)\n%)", mat);
>      writeln();
>
>      writefln("[%(%(%d %)\n%)]", mat);
>      writeln();
>
>      writefln("[%([%(%d %)]\n%)]", mat);
>      writeln();
> }
>
> Prints:
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> [1 2 3
> 4 5 6
> 7 8 9]
>
> [[1 2 3]
> [4 5 6]
> [7 8 9]
>
>
> Do you know why the last closed square bracket is missing?

You can use %| format specifier to specify element separator.

(It was proposed in 
https://github.com/D-Programming-Language/phobos/pull/298 .
It is not yet documented, but already merged in Phobos.)

import std.stdio;
void main() {
      auto mat = [[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]];

      writefln("[%([%(%d %)]%|\n%)]", mat);
      // specify "\n" as a separator
}

Prints:

[[1 2 3]
[4 5 6]
[7 8 9]]


More information about the Digitalmars-d-learn mailing list