Align columns: Difference between revisions
Content added Content deleted
(→{{header|UNIX Shell}}: Add column(1)-based implementation) |
No edit summary |
||
Line 1,494: | Line 1,494: | ||
Further allow for each word in a column to be either left |
Further allow for each word in a column to be either left |
||
justified right justified or center justified within its column.</pre> |
justified right justified or center justified within its column.</pre> |
||
=={{header|Beads}}== |
|||
<lang beads>beads 1 program 'Align columns' |
|||
const |
|||
text = '''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.''' |
|||
var |
|||
words : array^2 of str |
|||
widths : array of num |
|||
calc div_line |
|||
var s = '+' |
|||
loop across:widths val:w |
|||
s = s & str_repeat('-',w) & '+' |
|||
log s |
|||
calc show_table( |
|||
justify |
|||
) |
|||
loop across:words index:i |
|||
var s = '|' |
|||
loop across:widths index:j val:w |
|||
var |
|||
word = words[i,j] |
|||
if word == U |
|||
word = '' |
|||
case justify |
|||
| RIGHT |
|||
s = s & pad_left(word,w) |
|||
| LEFT |
|||
s = s & pad_right(word,w) |
|||
| CENTER |
|||
w = w - str_len(word) |
|||
s = s & str_repeat(' ',idiv(w,2)) & word & str_repeat(' ',idiv(w,2)+mod(w,2)) |
|||
s = s & '|' |
|||
log s |
|||
div_line |
|||
calc main_init |
|||
var w |
|||
split_lines_words (text, words, delim:'$') |
|||
loop across:words index:i |
|||
loop across:words[i] index:j |
|||
w = str_len(words[i,j]) |
|||
widths[j] = max(w,widths[j]) |
|||
loop across:[LEFT CENTER RIGHT] val:v |
|||
log "\n{v} justified\n" |
|||
div_line |
|||
show_table(v)</lang> |
|||
{{out}} |
|||
<pre> |
|||
LEFT justified |
|||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+ |
|||
|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.| | | | |
|||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+ |
|||
CENTER justified |
|||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+ |
|||
| 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.| | | | |
|||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+ |
|||
RIGHT justified |
|||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+ |
|||
| 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.| | | | |
|||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+ |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |