From: David Masover Date: 2009-09-29T00:28:36+09:00 Subject: Re: format problem Think about what's happening when you add a newline to that row. How will that newline be printed? Specifically, which printf statement are you using? On Monday 28 September 2009 08:38:49 am Li Chen wrote: > Hi all, > > I have an 2D array and I write it a file using the following script. But > when I open the written file I find the format is not what I want: the > second and third row are not justified as the first row. I wonder how to > correct it. > > > Thanks, > > Li > > ############ > c=[ ['a','b','c'],['c1',2,1],['d1',3,4] ] > c.collect!{|row| row<<"\n"} > > File.open('test.txt','w') do |a_file| > c.each do |row| > row.each {|e| ("#{e}"=~/[a-zA-Z]|\n/) ? > (a_file.printf("%s\t",e)) : ( a_file.printf("%.2f\t",e)) } > end > end > > ########output############# > > > a b c > c1 2.00 1.00 > d1 3.00 4.00 > > > ####expected as follow######## > a b c > c1 2.00 1.00 > d1 3.00 4.00