[Issue 9594] New: std.format.tableFormat
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Feb 26 04:32:07 PST 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9594
Summary: std.format.tableFormat
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2013-02-26 04:31:58 PST ---
writeln() format strings allows to format a 2D table in various ways:
import std.stdio: writeln, writefln;
void main() {
auto m = [[1, 100, 10], [1, 0, 10], [1, 100, 0]];
writefln("%(%(%3d %)\n%)", m);
writeln();
writefln("[%([%(%d, %)],\n %)]]", m);
writeln();
writefln("[%([%(%3d, %)],\n %)]]", m);
}
That output:
1 100 10
1 0 10
1 100 0
[[1, 100, 10],
[1, 0, 10],
[1, 100, 0]]
[[ 1, 100, 10],
[ 1, 0, 10],
[ 1, 100, 0]]
But if you want some vertical alignment you have to know the width in advance,
and all the table items get formatted with that width, leaving columns of
space.
So I suggest to introduce in std.format a simple function named tableFormat()
that given a range of ranges formats it in a nice 2D table:
[[1, 100, 10]
[1, 0, 10]
[1, 100, 0]]
In Mathematica it's named TableForm:
http://reference.wolfram.com/mathematica/ref/TableForm.html
It's handy in printing tables of numbers, but it's also useful for 2D arrays of
text:
auto mat =
[["Given", "a", "txt", "file", "of", "many", "lines,", "where", "fields",
"within", "a", "line"],
["are", "delineated", "by", "a", "single", "'dollar'", "character,", "write",
"a", "program"],
["that", "aligns", "each", "column", "of", "fields", "by", "ensuring", "that",
"words", "in", "each"],
["column", "are", "separated", "by", "at", "least", "one", "space."],
["Further,", "allow", "for", "each", "word", "in", "a", "column", "to", "be",
"either", "left"],
["justified,", "right", "justified,", "or", "center", "justified", "within",
"its", "column."]];
tableFormat!"-%"(mat, TableJustify.right) (the second argument defaults to
TableJustify.right) returns the string:
" Given a txt file of many lines, where
fields within a line"~
are delineated by a single 'dollar' character, write
a program"~
that aligns each column of fields by ensuring
that words in each"~
column are separated by at least one space."~
Further, allow for each word in a column
to be either left"~
justified, right justified, or center justified within its
column."
tableFormat!"-%"(mat, TableJustify.left) returns the string:
"Given a txt file of many lines, where
fields within a line"~
are delineated by a single 'dollar' character, write a
program"~
that aligns each column of fields by ensuring
that words in each"~
column are separated by at least one space. "~
Further, allow for each word in a column to
be either left"~
justified, right justified, or center justified within its
column."
tableFormat!"-%"(mat, TableJustify.center) returns the string:
" Given a txt file of many lines, where
fields within a line"~
are delineated by a single 'dollar' character, write
a program"~
that aligns each column of fields by ensuring
that words in each"~
column are separated by at least one space. "~
Further, allow for each word in a column
to be either left"~
justified, right justified, or center justified within its
column."
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list