3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # This program is public domain
7 # SEE: <http://rosettacode.org/wiki/Align_columns>
9 # Use `Text::justify` for the standard library.
12 fun aligner
(text
: String, left
: Float)
14 # Each row is a sequence of fields
15 var rows
= new Array[Array[String]]
16 for line
in text
.split
('\n') do
17 rows
.add line
.split
("$")
20 # Compute the final length of each column
21 var lengths
= new Array[Int]
24 for field
in fields
do
26 if lengths
.length
<= i
or fl
> lengths
[i
] then
33 # Process each line and align each field
35 var line
= new Array[String]
37 for field
in fields
do
38 line
.add field
.justify
(lengths
[i
], left
)
46 Given$a$text$file$of$many$lines,$where$fields$within$a$line$
47 are$delineated$by$a$single$'dollar'$character,$write$a$program
48 that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
49 column$are$separated$by$at$least$one$space.
50 Further,$allow$for$each$word$in$a$column$to$be$either$left$
51 justified,$right$justified,$or$center$justified$within$its$column."""