Align columns: Difference between revisions
Content added Content deleted
(→{{header|Prolog}}: Marked incorrect due to the presence of the extra quotes in the output.) |
|||
Line 1,468: | Line 1,468: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
⚫ | |||
{{incorrect|Prolog|Due to the presence of the extra quotes in the output.}} |
|||
⚫ | |||
Prolog has a particular treatment for strings : any string beginning by an uppercase letter is enclosed with quotes, ditto for a word ending with a comma or a point. |
|||
<lang Prolog>aligner :- |
<lang Prolog>aligner :- |
||
L ="Given$a$text$file$of$many$lines,$where$fields$within$a$line$ |
L ="Given$a$text$file$of$many$lines,$where$fields$within$a$line$ |
||
Line 1,484: | Line 1,482: | ||
% each line is a list of words |
% each line is a list of words |
||
parse(L, 0, N, LP, []), |
parse(L, 0, N, LP, []), |
||
% Because of the particular treatment of the strings by Prolog |
|||
% we need to add |
% we need to add 1 to aligned |
||
N1 is N+ |
N1 is N+1, |
||
% words will be left aligned |
% words will be left aligned |
||
sformat(AL, ' |
sformat(AL, '~~w~~t~~~w|', [N1]), |
||
% words will be centered |
% words will be centered |
||
sformat(AC, ' |
sformat(AC, '~~t~~w~~t~~~w|', [N1]), |
||
% words will be right aligned |
% words will be right aligned |
||
sformat(AR, ' |
sformat(AR, '~~t~~w~~~w|', [N1]), |
||
write('Left justified :'), nl, |
|||
maplist(affiche(AL), LP), nl, |
maplist(affiche(AL), LP), nl, |
||
write('Centered justified :'), nl, |
|||
maplist(affiche(AC), LP), nl, |
maplist(affiche(AC), LP), nl, |
||
write('Right justified :'), nl, |
|||
maplist(affiche(AR), LP), nl. |
maplist(affiche(AR), LP), nl. |
||
affiche(F, L) :- |
affiche(F, L) :- |
||
maplist( |
maplist(my_format(F), L), |
||
nl. |
nl. |
||
my_format(_F, [13]) :- |
|||
nl. |
nl. |
||
my_format(F, W) :- |
|||
string_to_atom(W,AW), |
|||
writef(F, [W]). |
|||
sformat(AF, F, [AW]), |
|||
write(AF). |
|||
Line 1,548: | Line 1,552: | ||
Output : |
Output : |
||
<lang Prolog> ?- aligner. |
<lang Prolog> ?- aligner. |
||
Left justified : |
|||
⚫ | |||
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 |
|||
⚫ | |||
Centered justified : |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
justified, right justified, or center justified within its column. |
|||
Right justified : |
|||
'Given' a text file of many 'lines,' where fields within a line |
|||
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. |
|||
true . |
|||
'Given' a text file of many 'lines,' where fields within a line |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
</lang> |
||