Align columns: Difference between revisions
Content added Content deleted
(→{{header|Snobol}}: Add implementation) |
|||
Line 7,851: | Line 7,851: | ||
|justified,| right |justified,| or |center|justified| within | its |column.| | | | |
|justified,| right |justified,| or |center|justified| within | its |column.| | | | |
||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+</pre> |
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+</pre> |
||
=={{header|Snobol}}== |
|||
{{works with|Snobol|4}} |
|||
<lang snobol>* Since we don't know how many lines or words we'll be reading in, |
|||
* store them in a table. |
|||
Words = TABLE() |
|||
* Pattern matches text from start of string to the first $ character |
|||
WordPat = POS(0) BREAK('$') . Word LEN(1) REM . Rest |
|||
* We output the results aligned three different ways; these are the |
|||
* labels for those sections of output: |
|||
Labels = ARRAY(3) |
|||
Labels<1> = "Left-justified" |
|||
Labels<2> = "Right-justified" |
|||
Labels<3> = "Centered" |
|||
* There are built-in functions for left- and right- justification, |
|||
* but not necessrily one for centering (depending on implementation). |
|||
* So we define one. |
|||
DEFINE('CPAD(Word,Width)Z,Left') :(CPAD_END) |
|||
CPAD Z = SIZE(Word) |
|||
Left = Z + (Width - Z) / 2 |
|||
CPAD = RPAD(LPAD(Word, Left), Width) :(RETURN) |
|||
CPAD_END |
|||
InLineLoop Line = INPUT :F(DoneReading) |
|||
LineCount = LineCount + 1 |
|||
Column = 0 |
|||
InWordLoop Column = Column + 1 |
|||
Line WordPat = Rest :S(CheckMax) |
|||
Word = Line |
|||
Line = |
|||
CheckMax GT(Column, MaxColumn) :F(StoreWord) |
|||
MaxColumn = Column |
|||
StoreWord Words<LineCount ',' Column> = Word |
|||
GT(Size(Line)) :S(InWordLoop)F(InLineLoop) |
|||
DoneReading |
|||
* Now we've read all the words in, find the field widths |
|||
Widths = ARRAY(MaxColumn) |
|||
J = 0 |
|||
FieldLoop J = J + 1 |
|||
GT(J, MaxColumn) :S(DoneCounting) |
|||
Widths<J> = 0 |
|||
I = 0 |
|||
CheckLineLoop I = I + 1 |
|||
GT(I, LineCount) :S(FieldLoop) |
|||
L = SIZE(Words<I ',' J>) :F(FieldLoop) |
|||
GT(L, Widths<J>) :F(CheckLineLoop) |
|||
Widths<J> = L :(CheckLineLoop) |
|||
DoneCounting |
|||
* Now we print the results out in the three styles |
|||
Style = 0 |
|||
StyleLoop Style = Style + 1 |
|||
GT(Style, 3) :S(END) |
|||
OUTPUT = |
|||
OUTPUT = Labels<Style> ':' |
|||
I = 0 |
|||
OutLineLoop I = I + 1 |
|||
GT(I, LineCount) :S(StyleLoop) |
|||
* Build up the output line by fields starting with the null string |
|||
Line = |
|||
J = 0 |
|||
OutWordLoop J = J + 1 |
|||
GT(J, MaxColumn) :S(PrintLine) |
|||
Word = Words<I ',' J> :F(PrintLine) |
|||
* Place the word within the column according to the pass we're on |
|||
EQ(Style, 1) :F(NotLeft) |
|||
* Left-justified |
|||
Word = RPAD(Word, Widths<J>) :(AddWord) |
|||
NotLeft EQ(Style, 2) :F(NotRight) |
|||
* Right-justified |
|||
Word = LPAD(Word, Widths<J>) :(AddWord) |
|||
* Centered |
|||
NotRight Word = CPAD(Word, Widths<J>) |
|||
* Add word to line and loop |
|||
AddWord Line = Line Word ' ' :(OutWordLoop) |
|||
* Print the line |
|||
PrintLine OUTPUT = Line :(OutLineLoop) |
|||
END</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. |
|||
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. |
|||
Centered: |
|||
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|Standard ML}}== |
=={{header|Standard ML}}== |