From: pjb@... (Pascal J. Bourguignon) Date: 2009-06-15T23:00:11+09:00 Subject: Re: formatting a listing George George writes: > (printf "%s\n\n" , > (begin > (matrix = (Array.new)) > (ncols = 1) > (matrix.push(Array.new)) > (((IO.readlines'/home/george/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)) > > 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') . map { | line | (line . strip) }) . each { | item | (if (item == "") (ncols = (ncols + 1)) (matrix . push(Array . new)) else ((matrix . at(ncols - 1)) . push item) end) }) ["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)) , ""))) }) . transpose) . map { | row | ((([ row , widths ] . transpose) . map { | r , w | (sprintf( "%*s" , w , r)) }) . join ",")}) . join "\n") "1,3,20\n2,4, 3\n3,5, 5\n , , 5" irb(main):918:0> (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 ((((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\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') . map { | line | (line . strip) }) . each { | item | (if (item == "") (ncols = (ncols + 1)) (matrix . push(Array . new)) else ((matrix . at(ncols - 1)) . push item) end) }) ["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)) , ""))) }) . transpose) . map { | row | ((([ row , widths ] . transpose) . map { | r , w | (sprintf( "%*s" , w , r)) }) . join ",")}) . join "\n") "1,3,20\n2,4, 3\n3,5, 5\n , , 5" irb(main):1042:0> -- __Pascal Bourguignon__