Align columns: Difference between revisions

Added XPL0 example.
(Add Easylang)
(Added XPL0 example.)
 
(5 intermediate revisions by 4 users not shown)
Line 1,341:
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">align←{
left ← {⍺↑⍵}
right ← {(-⍺)↑⍵}
center ← {⍺↑(-⌊(≢⍵)+(⍺-≢⍵)÷2)↑⍵}
text ← ⊃⎕NGET⍵
words ← ((≠∘'$')⊆⊣)¨(~text∊⎕TC)⊆text
sizes ← 1+⌈⌿↑≢¨¨words
method ← ⍎⍺
↑,/↑(⊂sizes)method¨¨↓↑words
}</syntaxhighlight>
{{out}}
<pre> 'left' align 'test.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.
'center' align 'test.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.
'right' align 'test.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>
=={{header|AppleScript}}==
 
Line 1,619 ⟶ 1,653:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column. </pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
Line 3,276 ⟶ 3,311:
{{trans|AWK}}
 
<syntaxhighlight lang=easylang>
global width inp$[] .
proc read . .
Line 3,289 ⟶ 3,324:
.
.
call read
#
proc out mode . .
Line 3,319 ⟶ 3,354:
.
.
call out 1
print ""
call out 2
print ""
call out 3
#
input_data
Line 3,332 ⟶ 3,367:
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column.
 
</syntaxhighlight>
 
Line 5,854 ⟶ 5,890:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
Alignment = {"left": -1, "center": 0, "right": 1}
 
Align = {}
Align.load = function(contents)
self.lines = contents.split(char(13))
self.rows = []
self.numColumns = 0
for line in self.lines
columns = line.split("$")
if columns.len > self.numColumns then self.numColumns = columns.len
self.rows.push(columns)
end for
self.widths = []
for col in range(0, self.numColumns - 1)
maxWidth = 0
for line in self.rows
if col > line.len - 1 then continue
if line[col].len > maxWidth then maxWidth = line[col].len
end for
self.widths.push(maxWidth)
end for
end function
 
Align.__getField = function(word, width, alignment)
if alignment == Alignment.left then return (word + " " * width)[:width]
if alignment == Alignment.right then return (" " * width+word)[-width:]
if alignment == Alignment.center then
leftMargin = floor((width - word.len) / 2)
return (" " * leftMargin + word + " " * width)[:width]
end if
end function
 
Align.output = function(alignment)
for line in self.rows
for c in range(0, line.len - 1)
print self.__getField(line[c], self.widths[c], alignment) + " ", ""
end for
print
end for
end function
 
txt = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" + char(13)
txt += "are$delineated$by$a$single$'dollar'$character,$write$a$program" + char(13)
txt += "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" + char(13)
txt += "column$are$separated$by$at$least$one$space." + char(13)
txt += "Further,$allow$for$each$word$in$a$column$to$be$either$left$" + char(13)
txt += "justified,$right$justified,$or$center$justified$within$its$column."
 
Align.load(txt)
print "Left alignment:"
Align.output(Alignment.left)
print
print "Right alignment:"
Align.output(Alignment.right)
print
print "Centered: "
Align.output(Alignment.center)
</syntaxhighlight>
 
 
{{out}}
<pre>
>miniscript.exe align-columns.ms
Left alignment:
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.
 
Right alignment:
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.
 
Centered:
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>
 
Line 8,940 ⟶ 9,067:
justified, right justified, or center justified within its column.
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program align;
magic := false; $ turn off regexp matching in GNU SETL
read_file;
ncols := max/[#line : line in lines];
sizes := [1+max/[#(line(col) ? "") : line in lines] : col in [1..ncols]];
loop for line in lines do
print(+/[align(line(col), sizes(col)) : col in [1..#line]]);
end loop;
read_file::
f := open(command_line(1), "r");
lines := [];
loop doing geta(f, line); while line /= om do
lines with:= split(line, "$");
end loop;
close(f);
proc align(s, n);
case command_line(2) of
("r"): return lpad(s, n);
("l"): return rpad(s, n);
("c"): return center(s, n);
end case;
end proc;
proc center(s, n);
padding := n - #s;
l := " " * ceil(padding/2);
r := " " * floor(padding/2);
return l + s + r;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>$ setl align.setl test.txt l
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.
$ setl align.setl test.txt r
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.
$ setl align.setl test.txt c
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|Shiny}}==
Line 10,577 ⟶ 10,762:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var LEFT = 0
Line 10,656 ⟶ 10,841:
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|XPL0}}==
<syntaxhighlight lang "XPL0">
 
string 0;
def LF=$0A, CR=$0D;
def Left, Right, Center;
 
proc AlignCols(S); \Display string S with its columns aligned
char S, C, Field(80), ColWidth(80);
int I, J, N, Just;
 
proc Justify;
int T;
 
proc SpOut(M); \Output M space characters
int M, K;
for K:= 0 to M-1 do ChOut(0, ^ );
 
proc FieldOut; \Output Field of N characters
int K;
for K:= 0 to N-1 do ChOut(0, Field(K));
 
[case Just of
Left: [FieldOut(N); SpOut(ColWidth(J)-N+1)];
Right: [SpOut(ColWidth(J)-N+1); FieldOut(N)];
Center:[T:= ColWidth(J)-N+1;
SpOut(T/2); FieldOut(N); SpOut(T/2 + rem(0))]
other [];
];
 
[\Get width (in characters) of each column
for J:= 0 to 80-1 do ColWidth(J):= 0;
I:= 0; J:= 0; N:= 0;
loop [repeat C:= S(I); I:= I+1 until C # CR;
if N > ColWidth(J) then ColWidth(J):= N;
case C of
0: quit;
^$: [N:= 0; J:= J+1];
LF: [N:= 0; J:= J+1; J:= 0]
other N:= N+1;
];
for Just:= Left to Center do
[I:= 0; J:= 0; N:= 0;
loop [repeat C:= S(I); I:= I+1 until C # CR;
case C of
0: [Justify(Just); CrLf(0); quit];
^$: [Justify(Just); N:= 0; J:= J+1];
LF: [Justify(Just); CrLf(0); N:= 0; J:= 0]
other [Field(N):= C; N:= N+1];
];
CrLf(0);
];
];
 
AlignCols("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.")
</syntaxhighlight>
{{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>
291

edits