From: John Nott Date: 2007-10-08T23:10:48+09:00 Subject: Re: MATRIX PROBLEMS (AGAIN!) Hi! This seems to work very well thanks but the results are produced in a long string. Is it possible to create these figures as a matrix? (resembling something like excel) Thanks for your help. - John -----Original Message----- From: list-bounce@example.com [mailto:list-bounce@example.com]On Behalf Of 7stud -- Sent: 08 October 2007 15:02 To: ruby-talk ML Subject: Re: MATRIX PROBLEMS (AGAIN!) 7stud -- wrote: > > But that is poorly written code; I can see that you copied it off the OT > website. Instead, you can write something like this: > > results = [] > > File.open("data.txt") do |file| > file.each_line(',') do |field| > results << field.to_i > end > end > > p results > -->[10, 20, 30, 50, 60] > Whoops. That doesn't work because the end of a line is not a comma so the last field on a line is combined into a string with the first field on the next line, which mucks up the results. You could do something like this instead: results = [] File.open("data.txt") do |file| file.each_line() do |record| fields = record.split(",") fields.each do |field| results << field.to_i end end end p results -- Posted via http://www.ruby-forum.com/. ##################################################################################### This e-mail message has been scanned for Viruses and Content and cleared by MailMarshal #####################################################################################