Align columns: Difference between revisions

1,729 bytes added ,  11 years ago
m (D entry: removed one import.)
Line 1,029:
 
</pre>
 
=={{header|FBSL}}==
<lang qbasic>#APPTYPE CONSOLE
 
DIM s = "Given$a$text$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."
 
DIM lines[], tokens[], l, t, length, margin, justify = "center"
 
lines = SPLIT(s, CRLF)
FOREACH l IN lines
tokens = SPLIT(l, "$")
FOREACH t IN tokens
IF STRLEN(t) > length THEN length = INCR(STRLEN)
NEXT
NEXT
 
FOREACH l IN lines
tokens = SPLIT(l, "$")
FOREACH t IN tokens
SELECT CASE justify
CASE "left"
PRINT t, SPACE(length - STRLEN(t));
CASE "center"
margin = (length - STRLEN(t)) \ 2
PRINT SPACE(margin), t, SPACE(length - STRLEN - margin);
CASE "right"
PRINT SPACE(length - STRLEN(t)), t;
END SELECT
NEXT
PRINT
NEXT
 
PAUSE</lang>
'''Output:'''
Given a text 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.
Press any key to continue...
 
=={{header|Factor}}==