Align columns: Difference between revisions

no edit summary
m (→‎{{header|Haskell}}: (Minor linting and import pruning of existing example))
No edit summary
Line 2,317:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.
</pre>
 
=={{header|Gambas}}==
<lang gambas>Public Sub Main() 'Written in Gambas 3.9.2 as a Command line Application - 15/03/2017
Dim siCount, siCounter, siLength As Short 'Counters
Dim siLongest As Short = -1 'To store the longest 'Word'
Dim sLine, sRows As New String[] 'Arrays
Dim sTemp, sAlign As String 'Temp strings
Dim sInput As String = "Given$a$text$file$of$many$lines, $where$fields$within$a$line$" & "\n"
"are$delineated$by$a$single$ 'dollar'$character,$write$a$program" & "\n"
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" & "\n"
"column$are$separated$by$at$least$one$space." & "\n"
"Further, $allow$for$each$word$in$a$column$to$be$either$left$" & "\n"
"justified, $right$justified, $or$center$justified$within$its$column." 'Main string (with End Of Line characters added)
 
For Each sTemp In Split(sInput, "\n") 'For each Line (split by End of Line character)..
sLine.add(sTemp) 'Add the Line to sLine array
Next
 
For siCount = 0 To sLine.Max 'For each of Lines in the array..
For Each sTemp In Split(sLine[siCount], "$") 'For each 'Word' in the Line (Split by the '$')
siLength = Len(sTemp) 'Store the length of the current 'Word'
If siLength > siLongest Then siLongest = siLength 'Make sure siLength has the length of the longest 'Word'
sRows.add(Trim(sTemp)) 'Create an array of the 'Words'
Next
sRows.add("\n") 'Add a End Of Line character to the sRows array
Next
 
For siCounter = 0 To 2 'For each alignment (Left, Right and Centre)
For Each sTemp In sRows 'For each 'Word' in the sRows array..
If sTemp = "\n" Then 'If it's a End Of Line character then..
Print 'Print
Continue 'Jump to the next iteration of the For Next Loop
Endif
If siCounter = 0 Then Print sTemp & Space(siLongest - Len(sTemp)); 'Print control for Left align
If siCounter = 1 Then Print Space(siLongest - Len(sTemp)) & sTemp; 'Print control for Right align
If siCounter = 2 Then 'Print control for Centre align
siCount = (siLongest - Len(sTemp)) / 2 'Difference between the length of the longest 'Word' and the current 'Word' / 2
sAlign = Space(siCount) & sTemp & Space(siCount) 'Put the string together for printing
If Len(sAlign) < siLongest Then sAlign &= " " 'Check it's the correct length if not add a space on the end
Print sAlign; 'Print the 'Word'
Endif
Next
Print 'Print an empty line between each alignment list
Next
 
End</lang>
 
{{out}}
<pre>
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.
 
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.
 
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>
 
Anonymous user