Talk:Align columns

From Rosetta Code
Revision as of 20:01, 1 December 2008 by rosettacode>DanBron (comments re: J solution)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

J solution

I haven't read the J solution in depth yet, but it appears more complex than may be neccesary. For example, with 'LEFT CENTER RIGHT'=:i.2then we can solve the task thus:

  (#~ 0 1 $~ #) (9!:17 CENTER #~ 2 [ 9!:7 ' '$~11) (]":) 4 :'<;._2&.(,&x)y'S:0 /'$';LF;text
  Given        a         text     file    of     many      lines,    where   fields within   a    line 
   are     delineated     by       a    single 'dollar'  character,  write     a                       
   that      aligns      each    column   of    fields       by     ensuring  that  words    in   each 
  column      are     separated    by     at     least      one                                        
 Further,    allow       for      each   word     in         a       column    to     be   either left 
justified,   right    justified,   or   center justified   within     its                              

Replacing CENTER with LEFT or RIGHT to taste.

Now, of course, the justification selected applies to every word. If we want to be able to treat each word (or column) independently, there are other methods. Even for this more general specification, I'm still not convinced that the large explicit verb is required, but I'll have to review it in detail to confirm.