# Formatting a listing

**URL:** <https://rubytalk.org/t/formatting-a-listing/53921>\
**Category:** ruby-talk\
**Created:** [15 June 2009 10:46 UTC](https://rubytalk.org/t/formatting-a-listing/53921 "2009-06-15T10:46:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [15 June 2009 10:46 UTC](https://rubytalk.org/t/formatting-a-listing/53921/1 "2009-06-15T10:46:34Z")

</div>

i have a listing which looks like this  
1  
2  
3

3  
4  
5

20  
3  
5  
5

i would like a quick way to format that output such that i end up with 3  
columns like the following.  
1,2,20  
2,4,3  
3,5,5  
&nbsp;&nbsp;&nbsp;&nbsp;5

any ideas?  
thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [15 June 2009 11:08 UTC](https://rubytalk.org/t/formatting-a-listing/53921/2 "2009-06-15T11:08:59Z")

</div>

If this is a homework assignement I am terribly sorry, but I cannot know.

puts DATA.inject( [] ){ | ary, ele |  
&nbsp;&nbsp;ele.strip.empty? ? ary \<\< : ary.last \<\< ele.to\_i # or ele.chomp  
if you prefer  
&nbsp;&nbsp;ary  
}  
.reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby  
.map{ | eles | eles.join( ", " )}

\_\_END\_\_  
1  
2

42  
44  
46

2000

HTH  
Robert

> **···**
>
> On Mon, Jun 15, 2009 at 12:46 PM, George George\<george.githinji@gmail.com\> wrote:
> 
> > i have a listing which looks like this  
> > 1  
> > 2  
> > 3
> > 
> > 3  
> > 4  
> > 5
> > 
> > 20  
> > 3  
> > 5  
> > 5
> > 
> > i would like a quick way to format that output such that i end up with 3  
> > columns like the following.  
> > 1,2,20  
> > 2,4,3  
> > 3,5,5  
> > 5
> > 
> > any ideas?  
> > thanks
> 
> --  
> Toutes les grandes personnes ont d’abord été des enfants, mais peu  
> d’entre elles s’en souviennent.
> 
> All adults have been children first, but not many remember.
> 
> [Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![Pascal\_Bourguignon](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Pascal\_Bourguignon](https://rubytalk.org/u/Pascal_Bourguignon)\
**Post date:** [15 June 2009 12:10 UTC](https://rubytalk.org/t/formatting-a-listing/53921/3 "2009-06-15T12:10:06Z")

</div>

George George \<george.githinji@gmail.com\> writes:

> i have a listing which looks like this  
> 1  
> 2  
> 3
> 
> 3  
> 4  
> 5
> 
> 20  
> 3  
> 5  
> 5
> 
> i would like a quick way to format that output such that i end up with 3  
> columns like the following.  
> 1,2,20  
> 2,4,3  
> 3,5,5  
> &nbsp;&nbsp;&nbsp;&nbsp;5
> 
> any ideas?

It's hard to tell, since there seem to be random changes in the output  
data vs. the input. Well, one random change. Or do you really mean  
to subtract 1 from the first row, second column of the output?  
What about the missing commas? Why the numbers are left-aligned?

(printf "%s\n\n" ,  
(begin  
&nbsp;&nbsp;&nbsp;&nbsp;(matrix = (Array . new))  
&nbsp;&nbsp;&nbsp;&nbsp;(ncols = 1)  
&nbsp;&nbsp;&nbsp;&nbsp;(matrix . push(Array . new))  
&nbsp;&nbsp;&nbsp;&nbsp;(((IO . readlines'/tmp/test.data') .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line . strip) }) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | (if (item == "")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ncols = (ncols + 1))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(matrix . push(Array . new))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((matrix . at(ncols - 1)) . push item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
&nbsp;&nbsp;&nbsp;&nbsp;(height = ((matrix . map { | column | (column . size) }) . max))  
&nbsp;&nbsp;&nbsp;&nbsp;(widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))  
&nbsp;&nbsp;&nbsp;&nbsp;((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transpose) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths] . transpose) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join ",")}) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join "\n")  
&nbsp;&nbsp;end))

1,3,20  
2,4, 3  
3,5, 5  
, , 5

> **···**
>
> --  
> \_\_Pascal Bourguignon\_\_

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [22 June 2009 13:04 UTC](https://rubytalk.org/t/formatting-a-listing/53921/4 "2009-06-22T13:04:47Z")

</div>

George George wrote:

> i have a listing which looks like this  
> 1  
> 2  
> 3
> 
> 3  
> 4  
> 5
> 
> 20  
> 3  
> 5  
> 5
> 
> i would like a quick way to format that output such that i end up with 3  
> columns like the following.  
> 1,2,20  
> 2,4,3  
> 3,5,5  
> &nbsp;&nbsp;&nbsp;&nbsp;5
> 
> any ideas?

src = "1\n2\n3\n\n3\n4\n5\n\n20\n3\n5\n5"  
rows = src.split("\n\n").collect { |b| b.split("\n") }  
m = rows.collect { |r| r.size }.max  
m.times { |i| puts rows.collect { |r| r[i] }.join(",") }

No witchcraft there.

I'd say it's better to use fastercsv gem for outputting, as it handles  
values which need quoting (e.g. values which themselves contain commas)

require 'rubygems'  
require 'fastercsv'  
src = "1\n2\n3\n\n3\n4\n5\n\n20\n3\n5,9\n5"  
rows = src.split("\n\n").collect { |b| b.split("\n") }  
m = rows.collect { |r| r.size }.max  
FCSV { |out| m.times { |i| out \<\< rows.collect { |r| r[i] } } }

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [15 June 2009 12:12 UTC](https://rubytalk.org/t/formatting-a-listing/53921/5 "2009-06-15T12:12:12Z")

</div>

Robert Dober wrote:

> > 3  
> > any ideas?  
> > thanks
> 
> If this is a homework assignement I am terribly sorry, but I cannot  
> know.
> 
> puts DATA.inject( [] ){ | ary, ele |  
> &nbsp;&nbsp;ele.strip.empty? ? ary \<\< : ary.last \<\< ele.to\_i # or ele.chomp  
> if you prefer  
> &nbsp;&nbsp;ary  
> }  
> .reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby  
> .map{ | eles | eles.join( ", " )}
> 
> \_\_END\_\_  
> 1  
> 2
> 
> 42  
> 44  
> 46
> 
> 2000
> 
> HTH  
> Robert

Thank so much for pointing the way: And this is not assignment! 🙂 Am  
working on some huge datasets that run in to up to 20 000 columns and  
was looking for an easy way of creating the columns.

puts DATA.inject( [] ){ | ary, ele |  
&nbsp;&nbsp;ele.strip.empty? ? ary \<\< : ary.last \<\< ele.to\_i # or ele.chomp  
#if you prefer  
# ary  
}  
#.reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby  
#.map{ | eles | eles.join( ", " )}

\_\_END\_\_  
1  
2

42  
44  
46

am not trying to ask too much but your code is real witchcraft 🙂  
raises an error  
`\<\<': can't convert Array into Integer (TypeError)

> **···**
>
> > On Mon, Jun 15, 2009 at 12:46 PM, George \> George\<george.githinji@gmail.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [15 June 2009 12:21 UTC](https://rubytalk.org/t/formatting-a-listing/53921/6 "2009-06-15T12:21:59Z")

</div>

Pascal J. Bourguignon wrote:

> George George \<george.githinji@gmail.com\> writes:
> 
> > 3  
> > any ideas?
> 
> It's hard to tell, since there seem to be random changes in the output  
> data vs. the input. Well, one random change. Or do you really mean  
> to subtract 1 from the first row, second column of the output?  
> What about the missing commas? Why the numbers are left-aligned?

thank you so much for the reply.  
The first column is a column of numbers separated by some white space.  
The idea is to capture the first block and create a column and the  
capture the next one and create another column adjacent to the previous  
one such that  
if  
1  
2  
3  
whitespace(one or more)  
3  
4  
5  
6  
whitespace(one or more)  
20  
2  
.  
.  
what i need is to transform that into (call it a matrix)  
such that every block of numbers separated by white space becomes a new  
row in the output e.g for he above case  
1,3,20  
2,4, 2  
3,5,  
, 6,

and so on. the comma separator is neccessary so that i can later parse  
it using any csv library or i can read it into a spreadsheet and

Thank you

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [15 June 2009 12:22 UTC](https://rubytalk.org/t/formatting-a-listing/53921/7 "2009-06-15T12:22:16Z")

</div>

> --  
> \_\_Pascal Bourguignon\_\_

Really Pascal, you do \*not\* need to sign your mails 😉

> **···**
>
> On Mon, Jun 15, 2009 at 2:10 PM, Pascal J. Bourguignon\<pjb@informatimago.com\> wrote:
> 
> --  
> Toutes les grandes personnes ont d’abord été des enfants, mais peu  
> d’entre elles s’en souviennent.
> 
> All adults have been children first, but not many remember.
> 
> [Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [22 June 2009 14:18 UTC](https://rubytalk.org/t/formatting-a-listing/53921/8 "2009-06-22T14:18:05Z")

</div>

Brian Candler wrote:

> George George wrote:

> > any ideas?
> 
> src = "1\n2\n3\n\n3\n4\n5\n\n20\n3\n5\n5"  
> rows = src.split("\n\n").collect { |b| b.split("\n") }  
> m = rows.collect { |r| r.size }.max  
> m.times { |i| puts rows.collect { |r| r[i] }.join(",") }
> 
> No witchcraft there.
> 
> I'd say it's better to use fastercsv gem for outputting, as it handles  
> values which need quoting (e.g. values which themselves contain commas)
> 
> require 'rubygems'  
> require 'fastercsv'  
> src = "1\n2\n3\n\n3\n4\n5\n\n20\n3\n5,9\n5"  
> rows = src.split("\n\n").collect { |b| b.split("\n") }  
> m = rows.collect { |r| r.size }.max  
> FCSV { |out| m.times { |i| out \<\< rows.collect { |r| r[i] } } }

Hi Brian!  
Thanks for the example however since i want to have the code read from a  
file and write to another file i have done this  
File.open("/home/george/output.csv","w") do |f|  
src = ""  
File.open('/home/george/sequences/results\_processed.csv').each do

> rows\>

&nbsp;&nbsp;src \<\< rows  
&nbsp;&nbsp;end  
&nbsp;&nbsp;rows = src.split("\n\n").collect { |b| b.split("\n") }  
&nbsp;&nbsp;m = rows.collect { |r| r.size }.max  
&nbsp;&nbsp;m.times { |i| f.puts rows.collect { |r| r[i] }.join(",") }

but it prints out the same old original file without modifications

Any ideas?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [15 June 2009 12:21 UTC](https://rubytalk.org/t/formatting-a-listing/53921/9 "2009-06-15T12:21:30Z")

</div>

If you have perf problems do not worry, inject is slow replace  
inject(x){ |a,e|  
}  
with  
loc = x  
each { |e|  
&nbsp;&nbsp;&nbsp;&nbsp;loc \<\< e # e.g  
}  
loc  
# you will have a speedup of 2~3

> **···**
>
> On Mon, Jun 15, 2009 at 2:12 PM, George George\<george.githinji@gmail.com\> wrote:
> 
> > Robert Dober wrote:
> > 
> > > On Mon, Jun 15, 2009 at 12:46 PM, George \>\> George\<george.githinji@gmail.com\> wrote:
> > > 
> > > > 3  
> > > > any ideas?  
> > > > thanks
> > > 
> > > If this is a homework assignement I am terribly sorry, but I cannot  
> > > know.
> > > 
> > > puts DATA.inject( [] ){ | ary, ele |  
> > > ele.strip.empty? ? ary \<\< : ary.last \<\< ele.to\_i # or ele.chomp  
> > > if you prefer  
> > > ary  
> > > }  
> > > .reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby  
> > > .map{ | eles | eles.join( ", " )}
> > > 
> > > \_\_END\_\_  
> > > 1  
> > > 2
> > > 
> > > 42  
> > > 44  
> > > 46
> > > 
> > > 2000
> > > 
> > > HTH  
> > > Robert
> > 
> > Thank so much for pointing the way: And this is not assignment! 🙂 Am  
> > working on some huge datasets that run in to up to 20 000 columns and  
> > was looking for an easy way of creating the columns.

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [15 June 2009 12:24 UTC](https://rubytalk.org/t/formatting-a-listing/53921/10 "2009-06-15T12:24:16Z")

</div>

.....  
such that every block of numbers separated by white space becomes a new  
row in the output e.g for he above case ....

what i meant was column not row(sorry)  
what i need is to transform that into (call it a matrix)  
such that every block of numbers separated by white space becomes a new  
column in the output e.g for he above case

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [15 June 2009 13:08 UTC](https://rubytalk.org/t/formatting-a-listing/53921/11 "2009-06-15T13:08:18Z")

</div>

(printf "%s\n\n" ,  
(begin  
&nbsp;&nbsp;&nbsp;&nbsp;(matrix = (Array.new))  
&nbsp;&nbsp;&nbsp;&nbsp;(ncols = 1)  
&nbsp;&nbsp;&nbsp;&nbsp;(matrix.push(Array.new))  
&nbsp;&nbsp;&nbsp;&nbsp;(((IO.readlines'/home/george/test.data').  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line.strip) }).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | (if (item == "")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ncols = (ncols + 1))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(matrix.push(Array.new))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(([matrix.at](http://matrix.at)(ncols - 1)).push item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
&nbsp;&nbsp;&nbsp;&nbsp;(height = ((matrix.map { | column | (column.size) }).max))  
&nbsp;&nbsp;&nbsp;&nbsp;(widths = (matrix.map { | column | ((column.map { | cell |  
((sprintf("%s" , cell)).size) }).max) }))  
&nbsp;&nbsp;&nbsp;&nbsp;((((matrix.map { | column | (column + (Array.new((height -  
(column.size)) , ""))) }) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transpose).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths].transpose).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }) .  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join ",")}).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join "\n")  
&nbsp;&nbsp;end))

produces an error:  
in `sprintf': no implicit conversion from nil to integer (TypeError)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [22 June 2009 18:59 UTC](https://rubytalk.org/t/formatting-a-listing/53921/12 "2009-06-22T18:59:57Z")

</div>

George George wrote:

> but it prints out the same old original file without modifications

It works for me (I just copy-pasted your code and changed the  
input/output filenames).

The simplest way to debug this is to add some extra debugging statements  
to see if the variables contain what you expect, for example:

&nbsp;&nbsp;...  
&nbsp;&nbsp;rows = src.split("\n\n").collect { |b| b.split("\n") }  
&nbsp;&nbsp;p rows # \<\< debugging statement

or more verbosely,

&nbsp;&nbsp;STDERR.puts "rows = #{rows.inspect}"

At a guess, perhaps you're on a Windows platform and the paragraphs are  
terminated by \r\n\r\n instead of \n\n. Try:

&nbsp;&nbsp;rows = src.split(/\r?\n\r?\n/).collect ... etc

In a regular expression, \r? means 0 or 1 occurrences of \r

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Pascal\_Bourguignon](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Pascal\_Bourguignon](https://rubytalk.org/u/Pascal_Bourguignon)\
**Post date:** [15 June 2009 14:00 UTC](https://rubytalk.org/t/formatting-a-listing/53921/13 "2009-06-15T14:00:11Z")

</div>

George George \<george.githinji@gmail.com\> writes:

> (printf "%s\n\n" ,  
> (begin  
> &nbsp;&nbsp;&nbsp;&nbsp;(matrix = (Array.new))  
> &nbsp;&nbsp;&nbsp;&nbsp;(ncols = 1)  
> &nbsp;&nbsp;&nbsp;&nbsp;(matrix.push(Array.new))  
> &nbsp;&nbsp;&nbsp;&nbsp;(((IO.readlines'/home/george/test.data').  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line.strip) }).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | (if (item == "")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ncols = (ncols + 1))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(matrix.push(Array.new))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((matrix.at(ncols - 1)).push item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
> &nbsp;&nbsp;&nbsp;&nbsp;(height = ((matrix.map { | column | (column.size) }).max))  
> &nbsp;&nbsp;&nbsp;&nbsp;(widths = (matrix.map { | column | ((column.map { | cell |  
> ((sprintf("%s" , cell)).size) }).max) }))  
> &nbsp;&nbsp;&nbsp;&nbsp;((((matrix.map { | column | (column + (Array.new((height -  
> (column.size)) , ""))) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transpose).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths].transpose).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join ",")}).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join "\n")  
> &nbsp;&nbsp;end))
> 
> produces an error:  
> in `sprintf': no implicit conversion from nil to integer (TypeError)

Well, I tried it with this file:

> **···**
>
> -----  
> 1  
> 2  
> 3
> 
> 3  
> 4  
> 5
> 
> 20  
> 3  
> 5  
> 5
> 
> -----
> 
> You may try to execute it expression per expression, and see where that nil comes from?
> 
> irb(main):850:0\>  
> (matrix = (Array . new))  
>   
> irb(main):856:0\>  
> (ncols = 1)  
> 1  
> irb(main):862:0\>  
> (matrix . push(Array . new))  
> []  
> irb(main):868:0\>  
> (((IO . readlines'/tmp/test.data') .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line . strip) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | (if (item == "")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ncols = (ncols + 1))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(matrix . push(Array . new))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((matrix . at(ncols - 1)) . push item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
> ["1", "2", "3", "", "3", "4", "5", "", "20", "3", "5", "5"]  
> irb(main):890:0\>  
> (height = ((matrix . map { | column | (column . size) }) . max))  
> 4  
> irb(main):896:0\>  
> (widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))  
> [1, 1, 2]  
> irb(main):902:0\>  
> ((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transpose) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths] . transpose) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join ",")}) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join "\n")  
> "1,3,20\n2,4, 3\n3,5, 5\n , , 5"  
> irb(main):918:0\>  
> (begin  
> &nbsp;&nbsp;&nbsp;&nbsp;(matrix = (Array . new))  
> &nbsp;&nbsp;&nbsp;&nbsp;(ncols = 1)  
> &nbsp;&nbsp;&nbsp;&nbsp;(matrix . push(Array . new))  
> &nbsp;&nbsp;&nbsp;&nbsp;(((IO . readlines'/tmp/test.data') .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line . strip) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | (if (item == "")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ncols = (ncols + 1))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(matrix . push(Array . new))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((matrix . at(ncols - 1)) . push item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
> &nbsp;&nbsp;&nbsp;&nbsp;(height = ((matrix . map { | column | (column . size) }) . max))  
> &nbsp;&nbsp;&nbsp;&nbsp;(widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))  
> &nbsp;&nbsp;&nbsp;&nbsp;matrix  
> &nbsp;&nbsp;&nbsp;&nbsp;((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transpose) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths] . transpose) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join ",")}) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join "\n")  
> &nbsp;&nbsp;end)  
> "1,3,20\n2,4, 3\n3,5, 5\n , , 5"  
> irb(main):968:0\>  
> (matrix = (Array . new))  
>   
> irb(main):974:0\>  
> (ncols = 1)  
> 1  
> irb(main):980:0\>  
> (matrix . push(Array . new))  
> []  
> irb(main):986:0\>  
> (((IO . readlines'/tmp/test.data') .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line . strip) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | (if (item == "")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ncols = (ncols + 1))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(matrix . push(Array . new))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((matrix . at(ncols - 1)) . push item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
> ["1", "2", "3", "", "3", "4", "5", "", "20", "3", "5", "5"]  
> irb(main):1005:0\>  
> matrix  
> [["1", "2", "3"], ["3", "4", "5"], ["20", "3", "5", "5"]]  
> irb(main):1008:0\>  
> (height = ((matrix . map { | column | (column . size) }) . max))  
> 4  
> irb(main):1014:0\>  
> (widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))  
> [1, 1, 2]  
> irb(main):1020:0\>  
> matrix  
> [["1", "2", "3"], ["3", "4", "5"], ["20", "3", "5", "5"]]  
> irb(main):1026:0\>  
> ((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transpose) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths] . transpose) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join ",")}) .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join "\n")  
> "1,3,20\n2,4, 3\n3,5, 5\n , , 5"  
> irb(main):1042:0\>
> 
> --  
> \_\_Pascal Bourguignon\_\_

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [23 June 2009 05:00 UTC](https://rubytalk.org/t/formatting-a-listing/53921/14 "2009-06-23T05:00:12Z")

</div>

Brian Candler wrote:

> At a guess, perhaps you're on a Windows platform and the paragraphs are  
> terminated by \r\n\r\n instead of \n\n. Try:
> 
> &nbsp;&nbsp;rows = src.split(/\r?\n\r?\n/).collect ... etc
> 
> In a regular expression, \r? means 0 or 1 occurrences of \r

Thank you Brian. Am on ubuntu linux and Ruby 1.8.7. Let me put some  
debugger and work it out why its not printing the desirable output.

@David Black:  
My input consists of digits and not letters, that is why i used digits  
in the examples above; in short what i really wanted is: Given a listing  
1  
2  
3  
white space  
3.05  
4  
5  
whitespace  
-6.3  
7

Produce an output such that:  
1,-3.05,-6.3  
2,4,7  
3,5

or  
1,-3.05,-6.3  
2,4,7  
3,5,0

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [19 June 2009 07:11 UTC](https://rubytalk.org/t/formatting-a-listing/53921/15 "2009-06-19T07:11:03Z")

</div>

Thanks a lot Pascal! The code is working though a little complex but a  
very nice learning piece of code. one nitty gritty thing is that not all  
the columns are seperated by a comma. for example your code

File.open("/home/george/test\_r.csv",'w') do |f|  
printf "%s\n\n",  
begin  
&nbsp;&nbsp;&nbsp;&nbsp;matrix = Array.new  
&nbsp;&nbsp;&nbsp;&nbsp;ncols = 1  
&nbsp;&nbsp;&nbsp;&nbsp;matrix.push(Array.new)  
&nbsp;&nbsp;&nbsp;&nbsp;(IO.readlines '/home/george/test.data').  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line.strip) }.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | if item == ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ncols = (ncols + 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.push(Array.new)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[matrix.at](http://matrix.at)(ncols - 1).push item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;height = (matrix.map { | column | (column.size) }).max

&nbsp;&nbsp;&nbsp;&nbsp;widths = matrix.map { | column | ((column.map { | cell |  
((sprintf("%s" , cell)).size) }).max) }

&nbsp;&nbsp;f.puts((matrix.map { | column | (column + (Array.new((height -  
column.size) , ""))) }).transpose.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths].transpose).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }).join ",")}.join "\n")  
end  
&nbsp;&nbsp;end

when given a file(test.data) containing  
1 2  
2 4

1 4  
2 3

1 3  
2 3  
3 5  
4 6

it produces (test\_r.csv)

1 2,1 4,1 3  
2 4,2 3,2 3  
&nbsp;&nbsp;&nbsp;, ,3 5  
&nbsp;&nbsp;&nbsp;, ,4 6

which is almost there!  
the ideal output would have been

1, 2,1, 4,1, 3  
2, 4,2, 3,2, 3  
, , ,3, 5  
, , ,4, 6

Sorry am asking for a horse and the saddle 🙂 what little modification  
should i do to produce the second output?  
Thank you so much!

George

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [23 June 2009 15:10 UTC](https://rubytalk.org/t/formatting-a-listing/53921/16 "2009-06-23T15:10:55Z")

</div>

> @David Black:  
> My input consists of digits and not letters, that is why i used digits  
> in the examples above; in short what i really wanted is: Given a listing  
> 1  
> 2  
> 3  
> white space  
> 3.05  
> 4  
> 5  
> whitespace  
> -6.3  
> 7
> 
> Produce an output such that:  
> 1,-3.05,-6.3

That's a spurious - before the 3, right?

> 2,4,7  
> 3,5
> 
> or  
> 1,-3.05,-6.3  
> 2,4,7  
> 3,5,0

The example that I couldn't quite figure out was the one with input  
like:

&nbsp;&nbsp;&nbsp;1 2,3 4

or something like that. Anyway, a simple version (which may not handle  
that case) is:

$/ = "\n\n" # if the input is definitely \n\n delimited

cols = File.open("input.csv") do |fh|  
&nbsp;&nbsp;&nbsp;max = 0  
&nbsp;&nbsp;&nbsp;fh.map do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row = s.scan(/\S+/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max = [max, row.size].max  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row \<\< "0" until row.size == max  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row  
&nbsp;&nbsp;&nbsp;end  
end

David

> **···**
>
> On Tue, 23 Jun 2009, George George wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now available: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> "Ruby 1.9: What You Need To Know" Envycasts with David A. Black  
> [http://www.envycasts.com](http://www.envycasts.com)

---

<div class="post-metadata">

**Author:** ![Pascal\_Bourguignon](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Pascal\_Bourguignon](https://rubytalk.org/u/Pascal_Bourguignon)\
**Post date:** [19 June 2009 11:35 UTC](https://rubytalk.org/t/formatting-a-listing/53921/17 "2009-06-19T11:35:05Z")

</div>

George George \<george.githinji@gmail.com\> writes:

> Thanks a lot Pascal! The code is working though a little complex but a  
> very nice learning piece of code. one nitty gritty thing is that not all  
> the columns are seperated by a comma. for example your code
> 
> File.open("/home/george/test\_r.csv",'w') do |f|  
> printf "%s\n\n",  
> begin  
> &nbsp;&nbsp;&nbsp;&nbsp;matrix = Array.new  
> &nbsp;&nbsp;&nbsp;&nbsp;ncols = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;matrix.push(Array.new)  
> &nbsp;&nbsp;&nbsp;&nbsp;(IO.readlines '/home/george/test.data').  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line.strip) }.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { | item | if item == ""  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ncols = (ncols + 1)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.push(Array.new)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.at(ncols - 1).push item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;height = (matrix.map { | column | (column.size) }).max
> 
> &nbsp;&nbsp;&nbsp;&nbsp;widths = matrix.map { | column | ((column.map { | cell |  
> ((sprintf("%s" , cell)).size) }).max) }
> 
> &nbsp;&nbsp;f.puts((matrix.map { | column | (column + (Array.new((height -  
> column.size) , ""))) }).transpose.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | row | ((([row , widths].transpose).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | r , w | (sprintf( "%\*s" , w , r)) }).join ",")}.join "\n")  
> end  
> &nbsp;&nbsp;end
> 
> when given a file(test.data) containing  
> 1 2  
> 2 4
> 
> 1 4  
> 2 3
> 
> 1 3  
> 2 3  
> 3 5  
> 4 6
> 
> it produces (test\_r.csv)
> 
> 1 2,1 4,1 3  
> 2 4,2 3,2 3  
> &nbsp;&nbsp;&nbsp;, ,3 5  
> &nbsp;&nbsp;&nbsp;, ,4 6
> 
> which is almost there!  
> the ideal output would have been
> 
> 1, 2,1, 4,1, 3  
> 2, 4,2, 3,2, 3  
> , , ,3, 5  
> , , ,4, 6
> 
> Sorry am asking for a horse and the saddle 🙂 what little modification  
> should i do to produce the second output?

You could add a split and a each somewhere.

irb(main):001:0\> "1 2".split(" ").each{|x| puts x}  
1  
2  
["1", "2"]

> **···**
>
> --  
> \_\_Pascal Bourguignon\_\_

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [22 June 2009 15:06 UTC](https://rubytalk.org/t/formatting-a-listing/53921/18 "2009-06-22T15:06:39Z")

</div>

Hi --

> Thanks a lot Pascal! The code is working though a little complex but a  
> very nice learning piece of code. one nitty gritty thing is that not all  
> the columns are seperated by a comma. for example your code
> 
> File.open("/home/george/test\_r.csv",'w') do |f|  
> printf "%s\n\n",  
> begin  
> &nbsp;&nbsp;&nbsp;matrix = Array.new  
> &nbsp;&nbsp;&nbsp;ncols = 1  
> &nbsp;&nbsp;&nbsp;matrix.push(Array.new)  
> &nbsp;&nbsp;&nbsp;(IO.readlines '/home/george/test.data').  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map { | line | (line.strip) }.

Don't use the extra parentheses around expressions. They result in  
unidiomatic and obfuscated code. They're an artifact of some very  
specific disgruntlement about the fact that Ruby differs from Common  
Lisp, and they shouldn't be emulated. (Search the ruby-talk archives  
for more on this if interested.)

> when given a file(test.data) containing  
> 1 2  
> 2 4
> 
> 1 4  
> 2 3
> 
> 1 3  
> 2 3  
> 3 5  
> 4 6
> 
> it produces (test\_r.csv)
> 
> 1 2,1 4,1 3  
> 2 4,2 3,2 3  
> &nbsp;&nbsp;, ,3 5  
> &nbsp;&nbsp;, ,4 6
> 
> which is almost there!  
> the ideal output would have been
> 
> 1, 2,1, 4,1, 3  
> 2, 4,2, 3,2, 3  
> , , ,3, 5  
> , , ,4, 6
> 
> Sorry am asking for a horse and the saddle 🙂 what little modification  
> should i do to produce the second output?

Could you write an example using letters, instead of digits? I'm  
having trouble mapping the input to the output, and having unique  
symbols would help.

Judging from the comma count, the last two rows have fewer fields than  
the first two. Is that right?

David

> **···**
>
> On Fri, 19 Jun 2009, George George wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now available: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> "Ruby 1.9: What You Need To Know" Envycasts with David A. Black  
> [http://www.envycasts.com](http://www.envycasts.com)

---

<div class="post-metadata">

**Author:** ![George\_George](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@George\_George](https://rubytalk.org/u/George_George)\
**Post date:** [23 June 2009 15:46 UTC](https://rubytalk.org/t/formatting-a-listing/53921/19 "2009-06-23T15:46:31Z")

</div>

David A. Black wrote:

> > whitespace  
> > -6.3  
> > 7
> > 
> > Produce an output such that:  
> > 1,-3.05,-6.3
> 
> That's a spurious - before the 3, right?
> 
> > 2,4,7  
> > 3,5
> > 
> > or  
> > 1,-3.05,-6.3  
> > 2,4,7  
> > 3,5,0
> 
> The example that I couldn't quite figure out was the one with input  
> like:
> 
> &nbsp;&nbsp;&nbsp;1 2,3 4
> 
> or something like that. Anyway, a simple version (which may not handle  
> that case) is:
> 
> $/ = "\n\n" # if the input is definitely \n\n delimited
> 
> cols = File.open("input.csv") do |fh|  
> &nbsp;&nbsp;&nbsp;max = 0  
> &nbsp;&nbsp;&nbsp;fh.map do |s|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row = s.scan(/\S+/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max = [max, row.size].max  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row \<\< "0" until row.size == max  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> David

Hi David!  
Thanks for the reply but the output given by the above code again  
only produces a single column instead of multiple columns. instead of  
given  
1  
2  
3  
white space  
3.05  
4  
5  
whitespace  
-6.3  
7

Produce an output such that:  
1,-3.05,-6.3  
2,4,7  
3,5

Please If you don't mind i can send you my actual input file off the  
list to try with. my email is georgkam hosted with the google email  
domain

> **···**
>
> > On Tue, 23 Jun 2009, George George wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [23 June 2009 17:24 UTC](https://rubytalk.org/t/formatting-a-listing/53921/20 "2009-06-23T17:24:46Z")

</div>

Hi --

> David A. Black wrote:
> 
> > > whitespace  
> > > -6.3  
> > > 7
> > > 
> > > Produce an output such that:  
> > > 1,-3.05,-6.3
> > 
> > That's a spurious - before the 3, right?
> > 
> > > 2,4,7  
> > > 3,5
> > > 
> > > or  
> > > 1,-3.05,-6.3  
> > > 2,4,7  
> > > 3,5,0
> > 
> > The example that I couldn't quite figure out was the one with input  
> > like:
> > 
> > &nbsp;&nbsp;&nbsp;1 2,3 4
> > 
> > or something like that. Anyway, a simple version (which may not handle  
> > that case) is:
> > 
> > $/ = "\n\n" # if the input is definitely \n\n delimited
> > 
> > cols = File.open("input.csv") do |fh|  
> > &nbsp;&nbsp;&nbsp;max = 0  
> > &nbsp;&nbsp;&nbsp;fh.map do |s|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row = s.scan(/\S+/)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max = [max, row.size].max  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row \<\< "0" until row.size == max  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > David
> 
> Hi David!  
> Thanks for the reply but the output given by the above code again  
> only produces a single column instead of multiple columns. instead of  
> given  
> 1  
> 2  
> 3  
> white space  
> 3.05  
> 4  
> 5  
> whitespace  
> -6.3  
> 7
> 
> Produce an output such that:  
> 1,-3.05,-6.3  
> 2,4,7  
> 3,5

Here's my input and output, along with the program:

$ cat george.rb  
$/ = "\n\n"

cols = File.open("input.csv") do |fh|  
&nbsp;&nbsp;&nbsp;max = 0  
&nbsp;&nbsp;&nbsp;fh.map do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row = s.scan(/\S+/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max = [max, row.size].max  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row \<\< "0" until row.size == max  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row  
&nbsp;&nbsp;&nbsp;end  
end

rows = cols.transpose.map {|row| row.join(",") }

puts rows  
$ cat input.csv  
1  
2  
3

3.05  
4  
5

-6.3  
7  
$ ruby george.rb 1,3.05,-6.3  
2,4,7  
3,5,0

The columns look OK. Note that the $/="\n\n" thing only works if your  
input is separated by exactly that sequence. That might be the problem  
you're having.

David

> **···**
>
> On Wed, 24 Jun 2009, George George wrote:
> 
> > > On Tue, 23 Jun 2009, George George wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now available: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> "Ruby 1.9: What You Need To Know" Envycasts with David A. Black  
> [http://www.envycasts.com](http://www.envycasts.com)

[Next page](https://rubytalk.org/t/formatting-a-listing/53921.md?page=2)
