From: "David A. Black" Date: 2009-06-23T00:06:39+09:00 Subject: Re: formatting a listing Hi -- On Fri, 19 Jun 2009, George George wrote: > > > Thanks a lot Pascal! The code is working though a little complex but a > very nice learning piece of code. one nitty gritty thing is that not all > the columns are seperated by a comma. for example your code > > File.open("/home/george/test_r.csv",'w') do |f| > printf "%s\n\n", > begin > matrix = Array.new > ncols = 1 > matrix.push(Array.new) > (IO.readlines '/home/george/test.data'). > map { | line | (line.strip) }. Don't use the extra parentheses around expressions. They result in unidiomatic and obfuscated code. They're an artifact of some very specific disgruntlement about the fact that Ruby differs from Common Lisp, and they shouldn't be emulated. (Search the ruby-talk archives for more on this if interested.) > when given a file(test.data) containing > 1 2 > 2 4 > > 1 4 > 2 3 > > 1 3 > 2 3 > 3 5 > 4 6 > > it produces (test_r.csv) > > 1 2,1 4,1 3 > 2 4,2 3,2 3 > , ,3 5 > , ,4 6 > > which is almost there! > the ideal output would have been > > 1, 2,1, 4,1, 3 > 2, 4,2, 3,2, 3 > , , ,3, 5 > , , ,4, 6 > > Sorry am asking for a horse and the saddle :) what little modification > should i do to produce the second output? Could you write an example using letters, instead of digits? I'm having trouble mapping the input to the output, and having unique symbols would help. Judging from the comma count, the last two rows have fewer fields than the first two. Is that right? David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com