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:
 
=={{header|Prolog}}==
Works with SWI-Prolog.<BR>
{{incorrect|Prolog|Due to the presence of the extra quotes in the output.}}
Works with SWI-Prolog.<BR>
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 :-
L ="Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 1,484 ⟶ 1,482:
% each line is a list of words
parse(L, 0, N, LP, []),
 
% Because of the particular treatment of the strings by Prolog
% we need to add 31 andto not 1aligned
N1 is N+31,
% words will be left aligned
sformat(AL, '%~wl~w~~t~~~w|', [N1]),
% words will be centered
sformat(AC, '%~wc~t~~w~~t~~~w|', [N1]),
% words will be right aligned
sformat(AR, '%~wr~t~~w~~~w|', [N1]),
 
write('Left justified :'), nl,
maplist(affiche(AL), LP), nl,
write('Centered justified :'), nl,
maplist(affiche(AC), LP), nl,
write('Right justified :'), nl,
maplist(affiche(AR), LP), nl.
 
affiche(F, L) :-
maplist(my_writefmy_format(F), L),
nl.
 
my_writefmy_format(_F, [13]) :-
nl.
 
my_writefmy_format(F, W) :-
string_to_atom(W,AW),
writef(F, [W]).
sformat(AF, F, [AW]),
write(AF).
 
 
Line 1,548 ⟶ 1,552:
Output :
<lang Prolog> ?- aligner.
Left justified :
'Given' a text file of many 'lines,' where fields within a line
areGiven a delineated by text file a of single many '''dollar''' 'character lines,' write where a fields within a line program
thatare aligns each column of fields delineated by a ensuring that single words 'dollar' character, inwrite a each program
columnthat arealigns each separated column by of at fields by ensuring that least words in one each 'space.'
'Further,'column allow are for each word in a column separated to by at be least either one left space.
'justifiedFurther,' right allow 'justified,'for or each center word justified in within its a 'column.' to be either left
'justified,' right right 'justified,' or center or center justified within withinits column. its 'column.'
 
Centered justified :
'Given' Given a a text file of many 'lines,' where fields within within a line
are are delineated by by a single a single '''dollar''' ' character,' write a a program program
that aligns each column of fields by by ensuring that words in in each each
column are are separated by at least one one 'space.'
' Further,' allow for each word in a column to be either left left
justified, right justified, or center justified within its column.
 
Right justified :
'Given' a text file of many 'lines,' where fields within a line
are Given delineated a by text afile of single '''dollar''' 'charactermany lines,' write where fields a within program a line
that are aligns each column of fieldsdelineated by ensuring that a wordssingle 'dollar' character, inwrite eacha program
column that arealigns separatedeach column by of atfields by ensuring that least words one in 'space.' each
'Further,' column allow are for each word in a column separated to by be at least either one left space.
'justified Further,' right allow 'justified,' for or each center word justified withinin its 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
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.'
</lang>