Align columns: Difference between revisions

Added XPL0 example.
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Added XPL0 example.)
 
(8 intermediate revisions by 5 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,272 ⟶ 3,307:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|AWK}}
 
<syntaxhighlight>
global width inp$[] .
proc read . .
repeat
inp$ = input
until inp$ = ""
inp$[] &= inp$
ar$[] = strsplit inp$ "$"
for s$ in ar$[]
width = higher width len s$
.
.
.
read
#
proc out mode . .
for inp$ in inp$[]
ar$[] = strsplit inp$ "$"
for s$ in ar$[]
spc = width - len s$ + 1
if mode = 1
write s$
for i to spc
write " "
.
elif mode = 2
for i to spc
write " "
.
write s$
elif mode = 3
for i to spc div 2
write " "
.
write s$
for i to spc - spc div 2
write " "
.
.
.
print ""
.
.
out 1
print ""
out 2
print ""
out 3
#
input_data
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>
 
=={{header|Elixir}}==
Line 5,793 ⟶ 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,879 ⟶ 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 9,255 ⟶ 9,501:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
 
pragma annotate( summary, "aligncols" )
@( description, "Given a text file of many lines, where fields within a line are delineated ")
@( description, "by a single 'dollar' character, write a program that aligns each column of" )
@( description, "fields by ensuring that words in each column are separated by at least one" )
@( description, "space. Further, allow for each word in a column to be either left justified," )
@( description, "right justified, or center justified within its column. " )
@( description, "A modified version of the Ada solution from Rosetta Code" )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Align_columns" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure aligncols is
Text : constant string :=
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$" & ASCII.NUL &
"are$delineated$by$a$single$'dollar'$character,$write$a$program" & ASCII.NUL &
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" & ASCII.NUL &
"column$are$separated$by$at$least$one$space." & ASCII.NUL &
"Further,$allow$for$each$word$in$a$column$to$be$either$left$" & ASCII.NUL &
"justified,$right$justified,$or$center$justified$within$its$column." & ASCII.NUL;
File : file_type;
Width : array(1..1000) of natural;
ch : character;
Column : positive := 1;
Start : positive := 1;
type Alignment is ( Left, Center, Right );
s : string;
padding : natural;
begin
-- Zero Widths
for I in arrays.first( Width )..arrays.last( Width ) loop
Width(I) := 0;
end loop;
-- Determining the widths of columns
for I in 1..strings.length(Text) loop
ch := strings.element( Text, I );
case ch is
when '$' | ASCII.NUL =>
Width (Column) := numerics.max(Width (Column), I - Start + 1);
Start := I + 1;
if strings.element( Text, I ) = ASCII.NUL then
Column := 1;
else
Column := @+1;
end if;
when others =>
null;
end case;
end loop;
create( File, out_file, "columned.txt" );
-- Formatting
for Align in Left..Right loop
Column := 1;
Start := 1;
for I in 1..strings.length(Text) loop
ch := strings.element( Text, I );
case ch is
when '$' | ASCII.NUL =>
s := strings.slice( Text, Start, I-1 );
padding := (Width( Column ) - strings.length(s));
case Align is
when Left =>
s := @ & (padding * ' ');
when Center =>
declare
left_padding : constant natural := padding/2;
right_padding : constant natural := padding - left_padding;
begin
s := (left_padding * ' ') & @ & (right_padding * ' ');
end;
when Right =>
s := (padding * ' ') & @;
when others =>
null;
end case;
put( File, s );
Start := I+1;
if ch = ASCII.NUL then
new_line( File );
Column := 1;
else
Column := @+1;
end if;
when others =>
null;
end case;
end loop;
new_line( File );
end loop;
close( File );
end aligncols;</syntaxhighlight>
 
=={{header|Standard ML}}==
Line 10,417 ⟶ 10,762:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var LEFT = 0
Line 10,496 ⟶ 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