Talk:Align columns: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎J solution: intentional data.)
Line 1: Line 1:
== J solution ==
== J solution ==


I haven't read the J solution in depth yet, but it appears more complex than may be necessary. For example, with <tt>'LEFT CENTER RIGHT'=:i.2</tt> we can solve the task thus:
I haven't read the J solution in depth yet, but it appears more complex than may be necessary. For example, with <tt>'LEFT CENTER RIGHT'=:i.3</tt> 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


(#~ 0 1 $~ #) (9!:17 CENTER #~ 2 [ 9!:7 ' '$~11) (]":) <;._2@:,~&> /'$';LF;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.
Replacing <tt>CENTER</tt> with <tt>LEFT</tt> or <tt>RIGHT</tt> to taste.
Replacing <tt>CENTER</tt> with <tt>LEFT</tt> or <tt>RIGHT</tt> to taste.


Line 20: Line 20:


:: I have added a note to clarify that this aspect of the data is intentional. --[[User:Paddy3118|Paddy3118]] 03:52, 2 December 2008 (UTC)
:: I have added a note to clarify that this aspect of the data is intentional. --[[User:Paddy3118|Paddy3118]] 03:52, 2 December 2008 (UTC)

::: Fixed. --[[User:DanBron|DanBron]] 12:38, 2 December 2008 (UTC)


== Output Question ==
== Output Question ==

Revision as of 12:38, 2 December 2008

J solution

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

   (#~ 0 1 $~ #) (9!:17 CENTER #~ 2 [ 9!:7 ' '$~11) (]":) <;._2@:,~&> /'$';LF;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.                      
                                                                                                           

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.

I concur that your solution is adequate to the specified task. It should replace the one I posted. I wrote mine without knowing about 9!:y. Thank you for this more skillful solution, Dan. --TBH 22:58, 1 December 2008 (UTC)
I've just noticed that the code proposed above does not compensate for the inconsistent input data, which lacks the delimiter at the end of some lines. (It does not seem right to me to have the example text have such an irregularity when nothing about the task suggests that data-quality testing is relevant, but, that's what we've been given.) --TBH 00:28, 2 December 2008 (UTC)
I have added a note to clarify that this aspect of the data is intentional. --Paddy3118 03:52, 2 December 2008 (UTC)
Fixed. --DanBron 12:38, 2 December 2008 (UTC)

Output Question

Given that the lines contain varying numbers of fields, when a justified line is output should the program
A) only output the justified fields that the line contains, or
B) output spaces for the columns when the line does not contain all the columns?

Hi, assume that the output should look good visually, and that lines will never be wrapped when displayed so trailing whitespace is irrelevant. --Paddy3118 03:39, 2 December 2008 (UTC)