Align columns: Difference between revisions
Content added Content deleted
m (→(with output): changed comments, simplified some code.) |
|||
Line 6,557: | Line 6,557: | ||
===(with output)=== |
===(with output)=== |
||
<lang rexx>/*REXX program displays various alignments for words in an array of text strings. */ |
<lang rexx>/*REXX program displays various alignments for words in an array of text strings. */ |
||
size= 0; t.=; cols= 0; wid.= 0; @.= /*zero or nullify some variables. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
t.3 = "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" |
|||
⚫ | |||
t. |
t.4 = "column$are$separated$by$at$least$one$space." |
||
⚫ | |||
t.4 = "column$are$separated$by$at$least$one$space." |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
_= strip(t.r,,'$') /*strip leading & trailing dollar signs*/ |
|||
do c=1 until _=='' /* [↓] process each of the words. */ |
|||
parse var _ @.r.c '$' _ /*extract the words from a line of text*/ |
|||
wid.c= max(wid.c, length(@.r.c) ) /*find the maximum word width.*/ |
|||
end /*c*/ |
|||
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; |
do k=1 for cols; size= size + wid.k; end /*find the width of the biggest line. */ |
||
rows=r- |
rows= r - 1 /*adjust ROWS because of the DO loop.*/ |
||
do j=1 for 3; say; |
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 |
do r=1 for rows; _= /*construct row by row. */ |
||
do c=1 for cols; x=@.r.c |
do c=1 for cols; x= @.r.c /* " col " col. */ |
||
if j==1 then _=_ left(x, wid.c) /*justified left. |
if j==1 then _= _ left(x, wid.c) /*justified left. */ |
||
if j==2 then _=_ right(x, wid.c) /* " right. |
if j==2 then _= _ right(x, wid.c) /* " right. */ |
||
if j==3 then _=_ |
if j==3 then _= _ center(x, wid.c) /* " center. */ |
||
end /*c*/ |
end /*c*/ |
||
say substr(_, 2) /*ignore the leading extra blank. */ |
say substr(_, 2) /*ignore the leading extra blank. */ |
||
end /*r*/ |
end /*r*/ |
||
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= when using the default input:}} |
{{out|output|text= when using the default input:}} |