Align columns: Difference between revisions
→{{header|Snobol}}: Just track column width as text is read in instead of in a second loop; improve comments.
(→{{header|Pascal}}: modified Delphi to run with free pascal added seperator | to show columns) |
(→{{header|Snobol}}: Just track column width as text is read in instead of in a second loop; improve comments.) |
||
Line 7,952:
=={{header|Snobol}}==
{{works with|Snobol|4}}
<lang snobol>* Since we don't know how
* we store
Words = TABLE()
*
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
* implementation). So we define one.
DEFINE('CPAD(Word,Width)Z,Left')
CPAD Z = SIZE(Word)
Left = Z + (Width - Z) / 2
CPAD = RPAD(LPAD(Word, Left), Width)
END_CPAD
InLineLoop Line = INPUT
InWordLoop Column = Column + 1
Line WordPat = Rest :S(CheckMax)▼
▲ Word = Line
* Separate Line into Word, Line=Rest at first dollar sign
MaxColumn = Column▼
* If there was no '$', the whole line is the next word
StoreWord Words<LineCount ',' Column> = Word▼
GT(Size(Line)) :S(InWordLoop)F(InLineLoop)▼
* Keep track of the largest number of columns on any line
▲ MaxColumn = Column
▲StoreWord Words<LineCount ',' Column> = Word
* And the size of the longest word in each column
Widths<Column> = SIZE(Word)
DoneReading
* Now we
StyleLoop Style = Style +
FieldLoop J = J + 1▼
▲ I = 0
GT(I, LineCount) :S(FieldLoop)▼
GT(L, Widths<J>) :F(CheckLineLoop)▼
*
*
* Left-justified
*
▲ GT(J, MaxColumn) :S(PrintLine)
▲ Word = Words<I ',' J> :F(PrintLine)
* Centered
NotRight Word = CPAD(Word, Widths<J>)▼
▲* Left-justified
▲ Word = RPAD(Word, Widths<J>) :(AddWord)
▲* Centered
▲NotRight Word = CPAD(Word, Widths<J>)
*
END
▲* Print the line
▲END</lang>
{{Out}}
|