From: David Masover Date: 2009-09-29T06:38:56+09:00 Subject: Re: format problem On Monday 28 September 2009 01:15:18 pm Li Chen wrote: > I think I do try to check if the element is a number by this code: > ("#{e}"=~/[a-zA-Z]|\n/) ? > (a_file.printf("%s\t",e)) : ( a_file.printf("%.2f\t",e)) That's not your issue. I did give you a hint: It's about the newlines. Now I should ask: Are you a newbie? If so, there may be some value in giving you a "hint" rather than just telling you what's wrong. Anyway, here's a hint: This line here: c.collect!{|row| row<<"\n"} What does c look like after that? Each row has a \n on it, right? If it helps, add this line immediately after that collect: p c Now, when you run that loop, each element that includes a letter (or \n) will run through this: a_file.printf("%s\t", e) What will that produce? For example,if you run 'a' through that, you'll get "a\t", right? So if you run "\n" through that, what do you get? Now, if you just want an answer, here's how I'd do it: c=[ ['a','b','c'],['c1',2,1],['d1',3,4] ] File.open('test.txt', 'w') do |a_file| c.each do |row| puts row.map{|e| e.kind_of?(Numeric) ? sprintf("%.2f", e) : e}.join("\t") end end Does that make sense?