John Nott wrote:
>
This code
file = File.new("data.txt")
fields = []
while (!file.eof)
line = file.gets
line.each(',') do |this_field|
fields << this_field.chomp(",").strip()
end
end
p fields
with this file:
data.txt:
10, 20, 30
40, 50, 60
produces this array:
-->["10", "20", "30", "40", "50", "60"]
(note that they are strings)
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]
But then, it's not clear why you even need a results array. Instead,
why not just insert each field straight into your matrix?
Also, in this code:
r = 1
n = 0
c = 1
line.each(',') { |this_record|
fields << this_record.chomp(",").strip()
mat1[r,c] = fields[r,c]
c=c+1
n=n+1
1) the variable this_record will actually be assigned a string like '3,'
so it's not a record--it's one field.
2) The value for r never changes.
--
Posted via http://www.ruby-forum.com/.