Align columns: Difference between revisions

Content added Content deleted
m (→‎(with output): changed comments, simplified some code.)
m (→‎(boxed output): added/changed come comments, added whitespace.)
Line 6,616: Line 6,616:


===(boxed output)===
===(boxed output)===
Note: This version boxes each column of output to better show the columns.
Note:   this version boxes each column of output to better show the columns.
<lang rexx>/*REXX pgm displays various (boxed) alignments for words in an array of text strings. */
<lang rexx>/*REXX program displays various alignments for words in an array of text strings. */
cols=0; size=0; wid.=0; t.=; @.= /*zero or nullify some variables. */
size= 0; t.=; cols= 0; wid.= 0; @.= /*zero or nullify some variables. */
t.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
/* [↓] some "text" lines. */
t.2 = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
t.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
t.3 = "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
t.2 = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
t.3 = "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
t.4 = "column$are$separated$by$at$least$one$space."
t.5 = "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
t.4 = "column$are$separated$by$at$least$one$space."
t.6 = "justified,$right$justified,$or$center$justified$within$its$column."
t.5 = "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
do r=1 while t.r\=='' /* [↓] process all the text lines. */
t.6 = "justified,$right$justified,$or$center$justified$within$its$column."
/* [↑] a null line is the end of text.*/
_= strip(t.r,,'$') /*strip leading & trailing dollar signs*/
do r=1 while t.r\=='' /* [↓] process all the text lines. */
do c=1 until _=='' /* [↓] process each of the words. */
_=strip(t.r,,'$') /*strip leading & trailing dollar signs*/
parse var _ @.r.c '$' _ /*extract the words from a line of text*/
do c=1 until _=='' /* [↓] process each of the words. */
wid.c= max(wid.c, length(@.r.c) ) /*find the maximum word width.*/
parse var _ @.r.c '$' _
end /*c*/
wid.c=max(wid.c, length(@.r.c)) /*find the maximum word width. */
cols= max(cols, c) /*use the maximum COLS found. */
end /*c*/
cols=max(cols,c) /*use the maximum COLS found. */
end /*r*/
end /*r*/


do k=1 for cols; size=size+wid.k; end /*find the width of the biggest line. */
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.*/
rows= r - 1 /*adjust ROWS because of the DO loop.*/
do j=1 for 3; say; say /*show two blank lines for a separator.*/
do j=1 for 3; say; say /*show two blank lines for a separator.*/
say center(word('left right center', j) "aligned", size+cols+1, "═") /*show title*/
say center(word('left right center', j) "aligned", size+cols+1, "═") /*show title*/


do r=0 to rows; _=; !='│'; if r==0 then !='┬'
do r=0 to rows; _=; != '│'; if r==0 then != '┬'
do c=1 for cols; x=@.r.c
do c=1 for cols; x= @.r.c
if r==0 then x=copies("─", wid.c +1)
if r==0 then x= copies("─", wid.c + 1)
if j==1 then _=_ || ! || left(x, wid.c)
if j==1 then _= _ || ! || left(x, wid.c) /*justify left*/
if j==2 then _=_ || ! || right(x, wid.c)
if j==2 then _= _ || ! || right(x, wid.c) /* " ruggt*/
if j==3 then _=_ || ! || centre(x, wid.c)
if j==3 then _= _ || ! || center(x, wid.c) /* " ctr.*/
end /*c*/
end /*c*/
if r==0 then do; _= '┌'substr(_, 2, length(_) -1)"┐"
if r==0 then do; _= '┌'substr(_, 2, length(_) - 1)"┐" /*top line.*/
bot= '└'substr(_, 2, length(_) -2)"┘"
bot= '└'substr(_, 2, length(_) - 2)"┘" /*bot " */
end
end
else _=_ || !
else _= _ || ! /*append trailing end boxing character.*/
say _
say _
end /*r*/ /* [↑] shows words in boxes. */
end /*r*/ /* [↑] shows words in boxes. */
say translate(bot, '┴', "┬")
say translate(bot, '┴', "┬") /*display the bottom line of the box. */
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/ /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}