Glenn Jackman wrote:
> At 2009-09-28 09:38AM, "Li Chen" wrote:
>>  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
> 
> You might try checking if the element is a number, otherwise treat it as
> a string:
> 
>     c.each do |row|
>       row.each do |elem|
>         fmt = Numeric === elem ? "%.2f\t" : "%s\t"
>         a_file.printf(fmt, elem)
>       end
>       a_file.puts
>     end


Hi Glen,

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))

It looks different from yours:
fmt = Numeric === elem ? "%.2f\t" : "%s\t"

So what is the real different here? Why do you  use a code line
Numeric===elem  ?

Thanks,

Li



-- 
Posted via http://www.ruby-forum.com/.