From: pjb@... (Pascal J. Bourguignon) Date: 2009-06-15T21:10:06+09:00 Subject: Re: formatting a listing George George 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 > 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 (matrix = (Array . new)) (ncols = 1) (matrix . push(Array . new)) (((IO . readlines'/tmp/test.data') . map { | line | (line . strip) }) . each { | item | (if (item == "") (ncols = (ncols + 1)) (matrix . push(Array . new)) else ((matrix . at(ncols - 1)) . push item) end) }) (height = ((matrix . map { | column | (column . size) }) . max)) (widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) })) ((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) . transpose) . map { | row | ((([ row , widths ] . transpose) . map { | r , w | (sprintf( "%*s" , w , r)) }) . join ",")}) . join "\n") end)) 1,3,20 2,4, 3 3,5, 5 , , 5 -- __Pascal Bourguignon__