Align columns: Difference between revisions

Content added Content deleted
No edit summary
(J: aesthetic changes)
Line 867:
=={{header|J}}==
'''Solution'''
<lang j>'LEFT CENTER RIGHT'=: i.3 NB. to specify NB. justification constants
 
NB.* alignCols v Format delimited text in justified columns
NB. y: text to format
NB. result: literal table
NB. rows marked by last character in text
NB. y is: literal list to format, fields delimited by $
NB. columns marked by $
NB. optional x is: optionally specify justification. Default is LEFT
NB. result: literal text table
alignCols=: verb define
LEFT alignCols y NB. specify default left argument where none given NB. default
:
global=. dyad def'9!:x y'each
currboxchars=. 9!:6 '' NB. store current settings so can restore later
oldbox=. 6 16 global '';'' NB. save settings
currboxalign=. 9!:16 ''
(9!:7) 17 global (11$#' ');,~x NB. set list of boxNB. charactersapply tonew spacessettings
resresult=. _2{:\ ": <;._2 @:(,&'$');._2 y NB. parse & format text
9!:7 17 ,~global oldbox x NB. set boxrestore alignmentsettings
res=. _2{:\ ": <;._2 @:(,&'$');._2 y NB. parse & format text
result
9!:17 currboxalign NB. restore settings
9!:7 currboxchars
res NB. return result
)</lang>