www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regarding writefln formatting

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Mar 20 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
 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
Mar 20 2012
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Mar 20 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Mar 20 2012
prev sibling parent "Kenji Hara" <k.hara.pg gmail.com> writes:
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]]
Mar 22 2012