Jump to content

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 many lines ormuch wordstext we'll be reading in,
* we store themthe inwords aand field widths in table.tables
Words = TABLE()
Word Widths = LineTABLE()
 
* Pattern matchesMatch text from start of string to the first $dollar charactersign
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 necessrilynecessarily one for centering (depending on implementation).
* implementation). So we define one.
DEFINE('CPAD(Word,Width)Z,Left') :(CPAD_ENDEND_CPAD)
 
CPAD Z = SIZE(Word)
Left = Z + (Width - Z) / 2
CPAD = RPAD(LPAD(Word, Left), Width) :(RETURN)
END_CPAD
CPAD_END
 
InLineLoop* Line = INPUT Read in our text a line at a time and split into words on :F(DoneReading)'$'
InLineLoop Line = INPUT LineCount = LineCount + 1:F(DoneReading)
Column LineCount = 0LineCount + 1
InWordLoop Column = Column += 10
InWordLoop Column = Column + 1
Line WordPat = Rest :S(CheckMax)
 
Word = Line
* Separate Line into Word, Line=Rest at first dollar sign
CheckMax GT(Column, MaxColumn) Line WordPat = Rest :FS(StoreWordCheckMax)
 
MaxColumn = Column
* If there was no '$', the whole line is the next word
StoreWord Words<LineCount ',' Column> = Word
I Word = 0Line
GT(Size(Line)) :S(InWordLoop)F(InLineLoop)
* Centered Line =
 
* Keep track of the largest number of columns on any line
CheckMax GTLE(JColumn, MaxColumn) :S(PrintLineStoreWord)
MaxColumn = Column
 
StoreWord Words<LineCount ',' Column> = Word
 
* And the size of the longest word in each column
Word = RPADGT(SIZE(Word), Widths<JColumn>) :F(AddWordNoNewMaxWidth)
Widths<Column> = SIZE(Word)
 
* Print Loop if the line isn't empty yet
NoNewMaxWidth GT(Size(Line)) :S(InWordLoop) F(InLineLoop)
DoneReading
 
* Now we've read allprint the wordsresults out in, find the fieldthree justification widthsstyles
Widths Style = ARRAY(MaxColumn)0
StyleLoop Style = Style + J = 01
Line WordPatGT(Style, 3) = Rest :S(CheckMaxEND)
FieldLoop J = J + 1
GT(J, MaxColumn)OUTPUT :S(DoneCounting)=
Widths<J> OUTPUT = 0 Labels<Style> ':'
 
I = 0
* Left-justified I = 0
CheckLineLoopOutLineLoop I = I + 1
GT(I, LineCount) :S(FieldLoop)
L = SIZEGT(Words<I ',' J>LineCount) :FS(FieldLoopStyleLoop)
GT(L, Widths<J>) :F(CheckLineLoop)
Widths<J> = L :(CheckLineLoop)
DoneCounting
 
* Now weBuild printup the resultsoutput outline inby fields starting with the threenull stylesstring
Style =Line 0=
StyleLoop Style = Style + 1 J = 0
FieldLoop OutWordLoop J = J + 1
GT(Style, 3) :S(END)
OUTPUT =GT(J, MaxColumn) :S(PrintLine)
OUTPUT Word = LabelsWords<Style>I ':,' J>
GT(I, LineCountSIZE(Word)) :SF(FieldLoopPrintLine)
 
* IPlace =the 0word within the column according to the pass we're on
GT EQ(LStyle, Widths<J>1) :F(CheckLineLoopNotLeft)
OutLineLoop I = I + 1
* Left-justified
GT(I, LineCount) :S(StyleLoop)
Word = Words<I 'RPAD(Word,' Widths<J> ) :F(PrintLineAddWord)
 
*NotLeft EQ(Style, 2) Build up the output line by fields starting with the null string :F(NotRight)
* Line =Right-justified
J Word = 0LPAD(Word, Widths<J>) :(AddWord)
OutWordLoop J = J + 1
GT(J, MaxColumn) :S(PrintLine)
Word = Words<I ',' J> :F(PrintLine)
 
* Centered
* Place the word within the column according to the pass we're on
NotRight Word = CPAD(Word, Widths<J>)
EQ(Style, 1) :F(NotLeft)
* Left-justified
Word = RPAD(Word, Widths<J>) :(AddWord)
 
NotLeft* EQ(Style, 2) Add word to line and :F(NotRight)loop
*AddWord Line = Line GT(SIZE(Line)) ' Right-justified'
Word Line = LPAD(Line Word, Widths<J>) :(AddWordOutWordLoop)
 
* Centered
NotRight Word = CPAD(Word, Widths<J>)
 
* Add wordPrint tothe line and loop
AddWordPrintLine LineOUTPUT = Line Word ' ' :(OutWordLoopOutLineLoop)
 
END
* Print the line
END</lang>
PrintLine OUTPUT = Line :(OutLineLoop)
END</lang>
 
{{Out}}
1,480

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.