Align columns: Difference between revisions

no edit summary
No edit summary
Line 4,883:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.</pre>
 
=={{header|Visual Basic}}==
<lang vb>Sub AlignCols(Lines, Optional Align As AlignmentConstants, Optional Sep$ = "$", Optional Sp% = 1)
Dim i&, j&, D&, L&, R&: ReDim W(UBound(Lines)): ReDim C&(0)
For j = 0 To UBound(W)
W(j) = Split(Lines(j), Sep)
If UBound(W(j)) > UBound(C) Then ReDim Preserve C(UBound(W(j)))
For i = 0 To UBound(W(j)): If Len(W(j)(i)) > C(i) Then C(i) = Len(W(j)(i))
Next i, j
 
For j = 0 To UBound(W): For i = 0 To UBound(W(j))
D = C(i) - Len(W(j)(i))
L = Choose(Align + 1, 0, D, D \ 2)
R = Choose(Align + 1, D, 0, D - L) + Sp
Debug.Print Space(L); W(j)(i); Space(R); IIf(i < UBound(W(j)), "", vbLf);
Next i, j
End Sub</lang>
Usage:<lang vb>Sub Main() 'usage of the above
Const Text$ = "Given$W$text$file$of$many$lines,$where$fields$within$W$line$" & vbLf & _
"are$delineated$by$W$single$'dollar'$character,$write$W$program" & vbLf & _
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" & vbLf & _
"column$are$separated$by$at$least$one$space." & vbLf & _
"Further,$allow$for$each$word$in$W$column$to$be$either$left$" & vbLf & _
"justified,$right$justified,$or$center$justified$within$its$column."
Debug.Print vbLf; "-- Left:": AlignCols Split(Text, vbLf), vbLeftJustify
Debug.Print vbLf; "-- Center:": AlignCols Split(Text, vbLf), vbCenter
Debug.Print vbLf; "-- Right:": AlignCols Split(Text, vbLf), vbRightJustify
End Sub</lang>
Output:<pre>-- Left:
Given W text file of many lines, where fields within W line
are delineated by W single 'dollar' character, write W 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 W column to be either left
justified, right justified, or center justified within its column.
 
-- Center:
Given W text file of many lines, where fields within W line
are delineated by W single 'dollar' character, write W 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 W column to be either left
justified, right justified, or center justified within its column.
 
-- Right:
Given W text file of many lines, where fields within W line
are delineated by W single 'dollar' character, write W 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 W column to be either left
justified, right justified, or center justified within its column. </pre>
 
=={{header|Visual Basic .NET}}==
Anonymous user