Align columns: Difference between revisions

Updated D entry
(Racket version)
(Updated D entry)
Line 702:
 
=={{header|D}}==
<lang d>import std.stdio, std.string, std.algorithm, std.array, std.typetuple;
 
void main() {
immutableauto Tdata =
"Given$a$txt$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
Line 711:
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.";
const P = array(.splitLines.map!q{ a.chomp("$").split("$") }(splitLines(T)));
 
const P = array(map!q{ a.chomp("$").split("$") }(splitLines(T)));
 
int[int] maxWidths;
foreach (line; Pdata)
foreach (i, word; line)
maxWidths[i] = max(maxWidths.get(i, 0), word.length);
 
foreach (i, justifyjust; [&leftJustifyTypeTuple!string(leftJustify, &center!string, rightJustify)) {
&rightJustify!string]) {
writeln(["Left", "Center", "Right"][i],
" column-aligned output:\n");
foreach (line; Pdata) {
foreach (j, word; line)
//write(word.justifyjust(word, maxWidths[j], ' '), " ");
write(justify(word, maxWidths[j], ' '), " ");
writeln();
}
writeln("- ".replicate(52).writeln();
 
writeln("- ".replicate(52));
}
}</lang>
{{out}}
Output:
<pre>Left column-aligned output: