Align columns: Difference between revisions

→‎{{header|q}}: q solution
(→‎{{header|q}}: q solution)
Line 6,295:
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column. </pre>
 
=={{header|q}}==
<lang q>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." )
 
ta:{[aln;txt] / tabulate aligned
sl:(count'')s:"$"vs/:txt; / strings and their lengths
tbl8:{0^y@/:\:x}[til max count each sl]; / tabulate
align:`L`R`C!( / alignment functions
{(1+max x)$'/:y}; / left
{(-1-max x)$'/:y}; / right
{mx:max x;(neg(mx-/:x)div 2)rotate''(1+mx)$'/:y} ); / center
1,[;"\n\n"]"\n"sv raze each(align aln). tbl8 each(sl;s); }</lang>
{{out}}
<lang q>q)print:{1,[;"\n\n"]"\n"sv raze each (align x) . tbl8 each(sl;s);}
q)ta `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.
 
q)ta `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.
 
q)ta `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.</lang>
[https://code.kx.com/q/ref/ Q Reference Card]
 
=={{header|R}}==
39

edits