Matrix problems (again!)

Hi John

Instead of this:
line.each(',') { |this_record|
  fields << this_record.chomp(",").strip() # read to the end of the
file into 'fields', split into lines and chomp out the delimiter
  mat1[r,c] = fields[r,c] # the rows and
columns in the matrix = rows and columns in fields
  c=c+1
  n=n+1
if c > 19 then # loops through
each row/column. At the end of each line where c=19, reset loop onto next row
with col = 1
c = 1
end
   }
which I think is the problem,

try

fields = eval("[" + line + "]")

this should turn the string into an array which you can iterate over - this comes from the Hal Fulton book so it should work.

HTH

Clive
---- John Nott <JNott@dto.ie> wrote:

···

Hi everyone,
As a complete new comer to Ruby. I have written this little piece of simple code but I have no idea why it only half works. It creates the matrix as I want it to but instead of populating it using the file as pointed to in file path, it fills it up with empty data!?

If anyone has any tips on where I may be going wrong I would really really appreciate it! I have remmed the code as best as I can throughout.

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' #the file that contains teh data that i would like to put into the matrix

file = File.new(filepath, 'r') # define new file as filepath

mat1 = OtMatrix.new(649,19) # define new omni trans matrix 649 rows, 19 columns

fields = Array.new # define fields as a new array to hold data

r = 1 # matrix row r = first row
n = 0 # n (fields) = 0
c = 1 # matric column 1 = first column

while ( !file.eof )
line = file.gets
line.each(',') { |this_record|
  fields << this_record.chomp(",").strip() # read to the end of the file into 'fields', split into lines and chomp out the delimiter
  mat1[r,c] = fields[r,c] # the rows and columns in the matrix = rows and columns in fields
  c=c+1
  n=n+1
if c > 19 then # loops through each row/column. At the end of each line where c=19, reset loop onto next row with col = 1
c = 1
end
   }
  
end

print mat1

Thanks very much in advance!

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