Creating Arrays

Dear All,<?xml:namespace prefix = o ns =
“urn:schemas-microsoft-com:office:office” />

Please forgive me if this email is a bit simple, but I am new to ruby
and object orientated programming pretty much and just looking for a few
tips from the experts.

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). Once I have achieved this, I will be making some updates to all
the figures within the Array.

At the moment my code looks like this:

print “Importing Planning Sheet: 2004_extrap_Planning_Data\n”

filepath = ‘Q:\TECH2006\DBA\02_LU\Final TAGM 2007
(working)\TAGM_666_Current_FROM
G_DRIVE\2004\2004_extrap_Planning_Data.csv’

Input_file = File.new(filepath, ‘r’)

puts “READING THE FILE”

fields = Array.new

while ( !Input_file.eof )

        line = Input_file.gets



        line.each(',') { |this_record|

                    fields << this_record.chomp(",").strip()

                     }

Input_file.each { |line| }

End

What I understand is happening is that I am reading my file into a new
Ruby file called “Input_file” and then I am creating a new Array called
“fields” and I am loading the new file into the fields array but
removing the commas using the chomp in each line. Unfortunately it isn’t
working!

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!

Any tips would be greatly appreciated!

Cheers!

  • John

#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared
by MailMarshal
#####################################################################################

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

2007/9/18, Ronald F. [email protected]:

table << fields
end

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

Kind regards

robert

On Sep 18, 2:39 am, “Ronald F.” [email protected]
wrote:

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