You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 %)
%)", m);
writeln();
writefln("[%([%(%d, %)],
%)]]", m);
writeln();
writefln("[%([%(%3d, %)],
%)]]", 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."
The text was updated successfully, but these errors were encountered:
bearophile_hugs reported this on 2013-02-26T04:31:58Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=9594
CC List
Description
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 %) %)", m); writeln(); writefln("[%([%(%d, %)], %)]]", m); writeln(); writefln("[%([%(%3d, %)], %)]]", 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."The text was updated successfully, but these errors were encountered: