[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Creating Arrays

Ronald Fischer

9/18/2007 7:39:00 AM

> 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".
Something like (code not tested!):

table=[] # create new array
open("your filename goes here").each do |line|
fields=[] # array of fields in each line
line.each(',') { |field|
fields << field.chomp(',').strip
}
table << fields
end
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162

2 Answers

Robert Klemme

9/18/2007 9:18:00 AM

0

2007/9/18, Ronald Fischer <ronald.fischer@venyon.com>:
> > 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".
> Something like (code not tested!):
>
> table=[] # create new array
> open("your filename goes here").each do |line|
> fields=[] # array of fields in each line
> line.each(',') { |field|
> fields << field.chomp(',').strip
> }
> table << fields
> end

Plus the reading / parsing could be done by a CSV library.

Kind regards

robert

William James

9/18/2007 9:32:00 AM

0

On Sep 18, 2:39 am, "Ronald Fischer" <ronald.fisc...@venyon.com>
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