Align columns: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, adjusted width of titles.
(Add Nit. Original author: Jean Privat <jean@pryen.org>)
m (→‎{{header|REXX}}: added/changed comments and whitespace, adjusted width of titles.)
Line 5,660:
 
===(with output)===
<lang rexx>/*REXX program displays various alignments for words in aan array of text stringstrings. */
cols=0; size=0; wid.=0; t.=; @.= /*zero| or nullify some variables. */
/* [↓] some "text" lines. */
t.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
t.2 = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
Line 5,669:
t.5 = "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
t.6 = "justified,$right$justified,$or$center$justified$within$its$column."
/* [↑] a null line is the end of text.*/
do r=1 while t.r\=='' /* [↓] process all the text lines. */
_=strip(t.r,,'$') /*strip leading & trailing $.dollar signs*/
do c=1 until _=='' /* [↓] process each wordof the words. */
parse var _ @.r.c '$' _
wid.c=max(wid.c, length(@.r.c)) /*maxfind the maximum word width. */
end /*c*/
cols=max(cols,c) /*use the maximum COLS found. */
end /*r*/
 
do k=1 for cols; size=size+wid.k; end /*find the width of the biggest line. */
rows=r-1 /*adjust ROWS because of the DO loop.*/
do j=1 for 3; say; say /*show 2two blank lines for sepseparater. */
say center(word('left right center', j) "aligned", size+cols-1, "═") /*show title*/
do r=1 for rows; _= /*construct row by row. [↑] shows words in boxes.*/
 
do do rc=1 for rowscols; _x= @.r.c /*build row by row " col " col. */
doif cj==1 forthen cols;_=_ left(x=@.r, wid.c) /*justified /*left. " col " col*/
if j==12 then _=_ leftright(x, wid.c) /*justified left " right. */
if j==23 then _=_ rightcentre(x, wid.c) /* " right center. */
if j==3 then _=_ centre(x, wid.c) /* " center.*/
end /*c*/
say substr(_, 2) /*ignore the leading extra blank. */
end /*r*/
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/
{{out|output|text=&nbsp; when using the default input:}}
/*stick a fork in it, we're done.*/</lang>
{{out}}
<pre>
══════════════════════════════════════════════left aligned══════════════════════════════════════════════
Line 5,725 ⟶ 5,723:
===(boxed output)===
Note: This version boxes each column of output to better show the columns.
<lang rexx>/*REXX programpgm displays various (boxed) alignments for words in aan array of text stringstrings. */
cols=0; size=0; wid.=0; t.=; @.= /*zero| or nullify some variables. */
/* [↓] some "text" lines. */
t.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
t.2 = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
Line 5,734 ⟶ 5,732:
t.5 = "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
t.6 = "justified,$right$justified,$or$center$justified$within$its$column."
/* [↑] a null line is the end of text.*/
do r=1 while t.r\=='' /* [↓] process all the text lines. */
_=strip(t.r,,'$') /*strip leading & trailing $.dollar signs*/
do c=1 until _=='' /* [↓] process each wordof the words. */
parse var _ @.r.c '$' _
wid.c=max(wid.c, length(@.r.c)) /*maxfind the maximum word width. */
end /*c*/
cols=max(cols,c) /*use the maximum COLS found. */
end /*r*/
 
do k=1 for cols; size=size+wid.k; end /*find the width of the biggest line. */
rows=r-1 /*adjust ROWS because of the DO loop.*/
do j=1 for 3; say; say /*show 2two blank lines for sepseparater. */
say center(word('left right center', j) "aligned", size+cols+1, "═") /*show title*/
 
do r=0 to rows; _=; !='│'; if r==0 then !='┬'
do c=1 for cols; x=@.r.c
if r==0 then x=copies("─", wid.c +1)
if j==1 then _=_ || ! || left(x, wid.c)
if j==2 then _=_ || ! || right(x, wid.c)
if j==3 then _=_ || ! || centre(x, wid.c)
end /*c*/
if r==0 then do; _= '┌'substr(_, 2, length(_) -1)"┐"
bot= '└'substr(_, 2, length(_) -2)"┘"
end
else _=_ || !
say _
end /*r*/ /* [↑] shows words in boxes. */
say translate(bot, '┴', "┬")
/* [↑] shows words in boxes.*/
end /*j*/ /*stick a fork in it, we're all done. */</lang>
say translate(bot,'┴',"┬")
{{out|output|text=&nbsp; when using the default input:}}
end /*j*/</lang>
{{out}}
<pre>
═══════════════════════════════════════════════left aligned═══════════════════════════════════════════════