From: William James Date: 2007-09-18T18:35:06+09:00 Subject: Re: Creating Arrays On Sep 18, 2:39 am, "Ronald Fischer" wrote: > > I have a .csz file created from a spreadsheet which contains > > 666 rows and 19 columns. What I would like to do is read the > > csz file into an array in ruby and store that array as a set > > of columns and rows (like in excel). > > [snip] > > > My question is basically how do I do what I want to do?... It > > is difficult for a new starter but I suspect you guys would > > know it inside out! > > Since you want to mimic the spreadsheet idiom (a table of rows > and columns), I suggest you create an "array of arrays". [...] class String def csv if include? '"' ary = "#{chomp},".scan( /\G"([^"]*(?:""[^"]*)*)",|\G([^,"]*),/ ) raise "Bad csv record:\n#{self}" if $' != "" ary.map{|a| a[1] || a[0].gsub(/""/,'"') } else ary = chomp.split( /,/, -1) ## "".csv ought to be [""], not [], just as ## ",".csv is ["",""]. if [] == ary [""] else ary end end end end array = nil File.open('junk'){|f| array = f.readlines.map{|s| s.csv } } p array