Align columns: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Ada solution added) |
(Added J.) |
||
Line 172: | Line 172: | ||
OD |
OD |
||
END</pre> |
END</pre> |
||
=={{header|J}}== |
|||
colalign=: verb define |
|||
'L' colalign y NB. default to left-justify all columns |
|||
: |
|||
afD=. ,&.> (#&D&.> @: (D&~: @: {: &.>)) NB. assure final delimiter (verb) |
|||
T0=. afD <;._2 y NB. text arranged as lines |
|||
T1=. > <;._2&.> T0 NB. as word grid |
|||
Alignment=. x, (({:$T1)-#x) # {:x |
|||
if. 'C' e. Alignment do. |
|||
Colwidths=. >./"2 Sizes=. > #&.>"0 T1 |
|||
CPad=. ('C'=Alignment)*"1 <.2%~ Sizes -~"1 Colwidths |
|||
T2=. T1 ,~&.>"0 CPad #&.>"0 ' ' NB. as center-justify-padded grid |
|||
else. T2=. T1 end. |
|||
if. doesR=. 'R' e. Alignment do. |
|||
whereR=.'R'=Alignment |
|||
T3=. (($T1)$whereR)}(,: |.&.>) T2 NB. as right-justify-prepared grid |
|||
else. T3=. T2 end. |
|||
T4=. >@{."1&.> 1 <@|:;.1 |: T3 NB. as columns |
|||
if. doesR do. |
|||
T5=. whereR }(,: |."1&.>) T4 NB. as rt-jstfy-completed columns |
|||
else. T5=. T4 end. |
|||
SP=. <|:,:' '#~#T0 NB. space-column to interleave with text columns |
|||
>,"1&.>/ }. ,|: SP,,:T5 |
|||
) |
|||
text=. noun define |
|||
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. |
|||
) |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |