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]]
17 for line
in text
.split
('\n') do
18 rows
.add line
.split
("$")
21 # Compute the final length of each column
22 var lengths
= new Array[Int]
25 for field
in fields
do
27 if lengths
.length
<= i
or fl
> lengths
[i
] then
34 # Process each line and align each field
36 var line
= new Array[String]
38 for field
in fields
do
39 line
.add field
.justify
(lengths
[i
], left
)
47 Given$a$text$file$of$many$lines,$where$fields$within$a$line$
48 are$delineated$by$a$single$'dollar'$character,$write$a$program
49 that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
50 column$are$separated$by$at$least$one$space.
51 Further,$allow$for$each$word$in$a$column$to$be$either$left$
52 justified,$right$justified,$or$center$justified$within$its$column."""