Thread overview
Regarding writefln formatting
Mar 21, 2012
bearophile
Mar 21, 2012
bearophile
Mar 21, 2012
Andrej Mitrovic
Mar 21, 2012
bearophile
Mar 22, 2012
Kenji Hara
March 21, 2012
This code:


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?

----------------------

The following is just a note. The formatting syntax for arrays is
rather powerful, it allows you to pretty print a matrix:

import std.stdio;
void main() {
     auto mat = [[1, 2, 3],
                 [4, 15, 6],
                 [7, 8, 9]];
     writefln("[%([%(%2d, %)],\n %)]]", mat);
}


That outputs:

[[ 1,  2,  3],
  [ 4, 15,  6],
  [ 7,  8,  9]]

But all the columns must have the same width, so the first and
third column waste space. So you can't write:

[[1,  2, 3],
  [4, 15, 6],
  [7,  8, 9]]

To do it you have to pre-process the matrix, and create a matrix
of already smartly formatted strings, or better write a pretty
printing function (that belongs in Phobos. Python has it in its
'pprint' standard library module).

Bye,
bearophile
March 21, 2012
> import std.stdio;
> void main() {
>      auto mat = [[1, 2, 3],
>                  [4, 15, 6],
>                  [7, 8, 9]];
>      writefln("[%([%(%2d, %)],\n %)]]", mat);
> }
>
>
> That outputs:
>
> [[ 1,  2,  3],
>   [ 4, 15,  6],
>   [ 7,  8,  9]]

Sorry, the dlang forum online interface has added an extra leading space on only certain lines.

I use 4 spaces indents in such code.

Bye,
bearophile
March 21, 2012
On 3/21/12, bearophile <bearophileHUGS@lycos.com> wrote:
> The following is just a note. The formatting syntax for arrays is rather powerful, it allows you to pretty print a matrix.

I didn't know that! Is this documented anywhere?
March 21, 2012
Andrej Mitrovic:

> I didn't know that! Is this documented anywhere?

It's documented formally, but I see no usage examples of the nested formatting syntax:
http://dlang.org/phobos/std_format.html

Bye,
bearophile
March 22, 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]]