Align columns: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎{{header|AppleScript}}: Innermost function slightly simpler – more declarative
PureFox (talk | contribs)
Added code for FreeBASIC
Line 2,107:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Sub Split(s As String, sep As String, result() As String)
Dim As Integer i, j, count = 0
Dim temp As String
Dim As Integer position(Len(s) + 1)
position(0) = 0
For i = 0 To Len(s) - 1
For j = 0 To Len(sep) - 1
If s[i] = sep[j] Then
count += 1
position(count) = i + 1
End If
Next j
Next i
position(count + 1) = Len(s) + 1
Redim result(count)
For i = 1 To count + 1
result(i - 1) = Mid(s, position(i - 1) + 1, position(i) - position(i - 1) - 1)
Next
End Sub
 
Sub CSet(buffer As String, s As Const String)
Dim As Integer bLength = Len(buffer)
Dim As Integer sLength = Len(s)
Dim As Integer diff, lSpaces
If sLength >= bLength Then
LSet buffer, s
Else
diff = bLength - sLength
lSpaces = diff \ 2
LSet buffer, Space(lSpaces) + s
End If
End Sub
Dim lines() As String
Dim count As Integer = 0
 
Open "align_columns.txt" For Input As #1
 
While Not Eof(1)
Redim Preserve lines(count)
Line Input #1, lines(count)
count +=1
Wend
 
Close #1
 
Dim As Integer i,j, length, numColumns = 0
Dim As Integer numLines = UBound(lines) + 1
Dim fields() As String
 
' Work out the maximum number of columns
For i = 0 To numLines - 1
Erase fields
Split RTrim(lines(i), "$"), "$", fields()
length = UBound(fields) + 1
If length > numColumns Then numColumns = length
Next
 
' Split lines into fields and work out maximum size of each column
Dim matrix(numLines - 1, numColumns - 1) As String
Dim columnSizes(numColumns - 1) As Integer
 
For i = 0 To numLines - 1
Erase fields
Split RTrim(lines(i), "$"), "$", fields()
For j = 0 To UBound(fields)
matrix(i, j) = fields(j)
length = Len(fields(j))
If length > columnSizes(j) Then columnSizes(j) = length
Next j
Next i
 
Dim buffer As String
 
'Separate each column by 2 spaces
Open "align_left_columns.txt" For Output As #1
Open "align_right_columns.txt" For Output As #2
Open "align_center_columns.txt" For Output As #3
 
For i = 0 To UBound(matrix, 1)
For j = 0 To UBound(matrix, 2)
buffer = Space(columnSizes(j))
LSet buffer, matrix(i, j)
Print #1, buffer;
RSet buffer, matrix(i, j)
Print #2, buffer;
CSet buffer, matrix(i, j)
Print #3, buffer;
If j < UBound(matrix, 2) Then
Print #1, " "; : Print #2, " "; : Print #3, " ";
End If
Next j
Print #1, : Print #2, : Print #3,
Next i
 
Close #1 : Close #2 : Close #3</lang>
 
{{out}}
<pre>--- align_columns.txt
 
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.
 
--- align_left_columns.txt
 
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.
 
--- align_right_columns.txt
 
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.
 
--- align_center_columns.txt
 
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.
</pre>