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

Reply via email to