53 lines
1.8 KiB
Plaintext
53 lines
1.8 KiB
Plaintext
Module Align_Columns {
|
|
a$={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.
|
|
}
|
|
const cr$=chr$(13), lf$=chr$(10)
|
|
def c1=0, cmax=0, p1=-1, i
|
|
flush ' empty stack
|
|
for i=1 to len(a$)
|
|
select case mid$(a$,i,1)
|
|
case "$", cr$
|
|
if p1<>-1 then data (p1, c1): p1=-1: cmax=max(c1,cmax):c1=0
|
|
case lf$
|
|
data (-1,0) ' push to end of stack an array of two items (a tuple in m2000)
|
|
else case
|
|
if p1=-1 then p1=i :c1=1 else c1++
|
|
end select
|
|
next
|
|
if p1<>-1 then push (p1, c1): cmax=max(c1,cmax):c1=0
|
|
\\ so now stack of values hold all tuples.
|
|
Dim Words(), AlignType$(1 to 3)
|
|
AlignType$(1)=lambda$ (a$,wd)->field$(a$, wd)
|
|
AlignType$(2)=lambda$ (a$,wd)->{
|
|
a$=left$(a$, wd)
|
|
=left$(string$(" ", (len(a$)-wd) div 2)+a$+string$(" ",wd),wd)
|
|
}
|
|
AlignType$(3)= lambda$ (a$,wd)->format$("{0:"+str$(-wd)+"}", a$)
|
|
\\ [] return a stack object, reference and leave current stack of values a new stack
|
|
\\ Array( stack_object) empty the stack object moving items to an array
|
|
Words()=Array([])
|
|
document export$
|
|
def aline$
|
|
cmax++ ' add one space
|
|
For al=1 to 3
|
|
For i=0 to len(Words())-1
|
|
if Words(i)(0)=-1 then
|
|
' we use rtrim$() to cut trailing spaces
|
|
export$=rtrim$(aline$)+cr$+lf$ : aline$=""
|
|
else
|
|
aline$+=AlignType$(al)(mid$(a$,Words(i)(0), Words(i)(1)),cmax)
|
|
end if
|
|
next i
|
|
next
|
|
\\ export to clipboard
|
|
Clipboard export$
|
|
Rem Form 140, 60
|
|
Rem Print #-2, export$ ' render text to console without using console's columns
|
|
}
|
|
Align_Columns
|