Align columns: Difference between revisions
Content added Content deleted
(→{{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: | Line 7,952: | ||
=={{header|Snobol}}== |
=={{header|Snobol}}== |
||
{{works with|Snobol|4}} |
{{works with|Snobol|4}} |
||
<lang snobol>* Since we don't know how |
<lang snobol>* Since we don't know how much text we'll be reading in, |
||
* store |
* we store the words and field widths in tables |
||
Words = TABLE() |
Words = TABLE() |
||
⚫ | |||
* |
* Match text from start of string to the first dollar sign |
||
WordPat = POS(0) BREAK('$') . Word LEN(1) REM . Rest |
WordPat = POS(0) BREAK('$') . Word LEN(1) REM . Rest |
||
* We output the results aligned three different ways; these are the |
* We output the results aligned three different ways; these are the |
||
* labels for those sections of output: |
* labels for those sections of output: |
||
Labels = ARRAY(3) |
Labels = ARRAY(3) |
||
Labels<1> = "Left-justified" |
Labels<1> = "Left-justified" |
||
Labels<2> = "Right-justified" |
Labels<2> = "Right-justified" |
||
Labels<3> = "Centered" |
Labels<3> = "Centered" |
||
* There are built-in functions for left- and right- justification, |
* There are built-in functions for left- and right- justification, |
||
* but not |
* but not necessarily one for centering (depending on |
||
* So we define one. |
* implementation). So we define one. |
||
DEFINE('CPAD(Word,Width)Z,Left') |
DEFINE('CPAD(Word,Width)Z,Left') :(END_CPAD) |
||
CPAD Z = SIZE(Word) |
CPAD Z = SIZE(Word) |
||
Left = Z + (Width - Z) / 2 |
Left = Z + (Width - Z) / 2 |
||
CPAD = RPAD(LPAD(Word, Left), Width) |
CPAD = RPAD(LPAD(Word, Left), Width) :(RETURN) |
||
END_CPAD |
|||
CPAD_END |
|||
* Read in our text a line at a time and split into words on '$' |
|||
InLineLoop Line = INPUT :F(DoneReading) |
|||
LineCount = LineCount + 1 |
|||
Column = 0 |
|||
InWordLoop Column = Column + 1 |
|||
⚫ | |||
⚫ | |||
Line = |
* Separate Line into Word, Line=Rest at first dollar sign |
||
Line WordPat = Rest :S(CheckMax) |
|||
⚫ | |||
* If there was no '$', the whole line is the next word |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
* Keep track of the largest number of columns on any line |
|||
⚫ | |||
⚫ | |||
⚫ | |||
* And the size of the longest word in each column |
|||
⚫ | |||
Widths<Column> = SIZE(Word) |
|||
⚫ | |||
⚫ | |||
DoneReading |
DoneReading |
||
* Now we |
* Now we print the results out in the three justification styles |
||
Style = 0 |
|||
StyleLoop Style = Style + 1 |
|||
⚫ | |||
⚫ | |||
OUTPUT = |
|||
OUTPUT = Labels<Style> ':' |
|||
⚫ | |||
⚫ | |||
OutLineLoop I = I + 1 |
|||
⚫ | |||
GT(I, LineCount) :S(StyleLoop) |
|||
⚫ | |||
Widths<J> = L :(CheckLineLoop) |
|||
DoneCounting |
|||
* |
* Build up the output line by fields starting with the null string |
||
Line = |
|||
J = 0 |
|||
⚫ | |||
GT(Style, 3) :S(END) |
|||
GT(J, MaxColumn) :S(PrintLine) |
|||
Word = Words<I ',' J> |
|||
⚫ | |||
* Place the word within the column according to the pass we're on |
|||
⚫ | |||
OutLineLoop I = I + 1 |
|||
* Left-justified |
|||
GT(I, LineCount) :S(StyleLoop) |
|||
⚫ | |||
NotLeft EQ(Style, 2) :F(NotRight) |
|||
* Right-justified |
|||
Word = LPAD(Word, Widths<J>) :(AddWord) |
|||
OutWordLoop J = J + 1 |
|||
⚫ | |||
⚫ | |||
* Centered |
|||
* Place the word within the column according to the pass we're on |
|||
⚫ | |||
EQ(Style, 1) :F(NotLeft) |
|||
⚫ | |||
⚫ | |||
* Add word to line and loop |
|||
AddWord Line = Line GT(SIZE(Line)) ' ' |
|||
Line = Line Word :(OutWordLoop) |
|||
⚫ | |||
⚫ | |||
* |
* Print the line |
||
PrintLine OUTPUT = Line :(OutLineLoop) |
|||
END |
|||
⚫ | |||
⚫ | |||
PrintLine OUTPUT = Line :(OutLineLoop) |
|||
⚫ | |||
{{Out}} |
{{Out}} |