Jump to content

Align columns: Difference between revisions

(Updated D entry)
Line 3,596:
(display-aligned #:justify 'right text)
(display-aligned #:justify 'center text)
</lang>
 
=={{header|RapidQ}}==
<lang vb>
Dim MText as QMemorystream
MText.WriteLine "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
MText.WriteLine "are$delineated$by$a$single$'dollar'$character,$write$a$program"
MText.WriteLine "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
MText.WriteLine "column$are$separated$by$at$least$one$space."
MText.WriteLine "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
MText.WriteLine "justified,$right$justified,$or$center$justified$within$its$column."
 
DefStr TextLeft, TextRight, TextCenter
DefStr Line, AWord, LWord, Newline = chr$(13)+chr$(10)
DefInt ColWidth(100), ColCount
DefSng NrSpaces
 
'Find column widths
MText.position = 0
for x = 0 to MText.linecount -1
Line = MText.ReadLine
for y = 0 to Tally(Line, "$")
LWord = Field$(Line, "$", y+1)
ColWidth(y) = iif (ColWidth(y) < len(LWord), len(LWord), ColWidth(y))
next
next
 
'Create aligned wordlists
MText.position = 0
for x = 0 to MText.linecount -1
Line = MText.ReadLine
for y = 0 to Tally(Line, "$")
LWord = Field$(Line, "$", y+1)
'left align
AWord = LWord + String$(ColWidth(y) - len(LWord) +1, " ")
TextLeft = TextLeft + AWord
'Right align
AWord = String$(ColWidth(y) - len(LWord) +1, " ") + LWord
TextRight = TextRight + AWord
'Center
NrSpaces = (ColWidth(y) - len(LWord))/2
AWord = String$(floor(NrSpaces)+1, " ") + LWord + string$(Ceil(NrSpaces), " ")
TextCenter = TextCenter + AWord
next
TextLeft = TextLeft + Newline
TextRight = TextRight + Newline
TextCenter = TextCenter + Newline
next
</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.