Align columns: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
No edit summary |
||
Line 7,035: | Line 7,035: | ||
?></lang> |
?></lang> |
||
=={{header|Picat}}== |
|||
<lang Picat>import util. |
|||
main => |
|||
Text = |
|||
"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.", |
|||
Lines = split(Text,"\n"), |
|||
Lines = [Line1|_], |
|||
N = len(split(strip(Line1,"$ "), "$")), % number of columns |
|||
WidthArr = {0 : _ in 1..N}, |
|||
foreach (Line in Lines) |
|||
Words = split(strip(Line,"$ "), "$"), |
|||
foreach ({I,Word} in zip(1..N, Words)) |
|||
WidthArr[I] := max(WidthArr[I], len(Word)) |
|||
end |
|||
end, |
|||
foreach (I in 1..N) |
|||
WidthArr[I] := WidthArr[I]+1 % separate cols by at least one space |
|||
end, |
|||
foreach (Align in [left, right, center]) |
|||
output_lines(Lines,N,WidthArr,Align), |
|||
nl,nl |
|||
end. |
|||
output_lines(Lines,N,WidthArr,Align) => |
|||
foreach (Line in Lines) |
|||
Words = split(strip(Line,"$ "), "$"), |
|||
foreach ({I,Word} in zip(1..N, Words)) |
|||
output_word(Word,WidthArr[I],Align) |
|||
end, |
|||
nl |
|||
end. |
|||
output_word(Word,Width,left) => |
|||
printf("%-*s",Width,Word). |
|||
output_word(Word,Width,right) => |
|||
printf("%*s",Width,Word). |
|||
output_word(Word,Width,_) => |
|||
Pad = len(Word)-Width, |
|||
Pad1 is Pad div 2, |
|||
Pad2 is Pad-Pad1, |
|||
printf("%*s%s%*s",Pad1,"",Word,Pad2,""). |
|||
</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> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |