Align columns: Difference between revisions

m (→‎{{header|REBOL}}: Fixed output paste.)
Line 1,756:
 
=={{header|REXX}}==
===version 1===
<lang rexx>z.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
<lang rexx>
z.1 = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
z.2 = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
z.3 = "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
Line 1,804 ⟶ 1,806:
say out
end</lang>
===version 2===
<lang rexx>
/*REXX program to display various alignments. */
cols=0; size=0; wid.=0; t.=''; @.=''
 
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"
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.6="justified,$right$justified,$or$center$justified$within$its$column."
 
do r=1 while t.r\==''
_=strip(t.r,,'$')
do c=1 until _==''
parse var _ @.r.c '$' _
wid.c=max(wid.c,length(@.r.c))
end /*c*/
cols=max(cols,c)
end /*r*/
 
rows=r-1 /*adjust ROWS, it's 1 too big*/
do j=1 for cols; size=size+wid.j; end /*find width of biggest line.*/
 
do j=1 for 3
say; say center(word('left right center',j) "aligned",size+cols-1,"=")
do r=1 for rows; _=''
do c=1 for cols; x=@.r.c
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*/
say substr(_,2)
end /*r*/
say
end /*j*/
</lang>
Output:
<pre style="height:30ex;overflow:scroll">
 
==============================================left aligned==============================================
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 aligned==============================================
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 aligned=============================================
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>
===version 3===
<lang rexx>
/*REXX program to display various alignments. */
cols=0; parse var cols size 1 wid. t. /*initializations.*/
 
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"
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.6="justified,$right$justified,$or$center$justified$within$its$column."
 
do r=1 while t.r\==''
t.r=translate(t.r,,'$')
do c=1 until word(t.r,c)==''
wid.c=max(wid.c,length(word(t.r,c)))
end
cols=max(cols,c)
end
 
rows=r-1 /*adjust ROWS, it's 1 too big*/
do j=1 for cols; size=size+wid.j; end /*find width of biggest line.*/
 
do j=1 for 3
say;say center(word('left right center',j) "aligned",size+cols,"=");say
do r=0 to rows; _=''
!='|'; if r==0 then !='+'
do c=1 for cols;
x=word(t.r,c)
if r==0 then x=copies("-",wid.c+1)
else x=word(t.r,c)
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 bot=_
say _
end /*r*/
say bot; say; say
end /*j*/
</lang>
Output:
<pre style="height:30ex;overflow:scroll">
 
===============================================left aligned===============================================
 
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+
|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 aligned===============================================
 
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+
| 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 aligned==============================================
 
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+
| 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|Ruby}}==