Matrix problems (again!)

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
#####################################################################################

John N. 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.

7stud – wrote:

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]

Whoops. That doesn’t work because the end of a line is not a comma so
the last field on a line is combined into a string with the first field
on the next line, which mucks up the results. You could do something
like this instead:

results = []

File.open(“data.txt”) do |file|
file.each_line() do |record|
fields = record.split(",")
fields.each do |field|
results << field.to_i
end
end
end

p results

7stud – wrote:

results = []

File.open(“data.txt”) do |file|
file.each_line() do |record|
fields = record.split(",")
fields.each do |field|
results << field.to_i
end
end
end

p results

You can also use the Standard Library module CSV(or get the gem
FasterCSV) to easily read comma separated data:

require ‘csv’

matrix = []

CSV.open(‘data.txt’, ‘r’) do |row|
row.each do |column|
matrix << column.to_i
end
end

p matrix
–>[10, 20, 30, 40, 50, 60]

Hi!
This seems to work very well thanks but the results are produced in a
long string. Is it possible to create these figures as a matrix?
(resembling something like excel)
Thanks for your help.

  • John

John N. wrote:

Hi!
This seems to work very well thanks but the results are produced in a
long string. Is it possible to create these figures as a matrix?
(resembling something like excel)
Thanks for your help.

  • John

Here are some guidelines:

  1. Start off writing a program that works on a small amount of data,
    e.g. a file with this in it:

10,20,30
40,50,60

  1. Always post an example of the format of your data, so we all know
    what you are dealing with.

  2. Clearly state what the results should be. At this point I still
    don’t have any idea whether you want integers in your matrix or strings.

  3. State what you intend to do with the result, so that people will have
    a better idea of what you really need rather that what you say you
    want.

John N. wrote:

Good Points!!! (hope my explanation makes sense!)

  1. Start off writing a program that works on a small amount of data,
    e.g. a file with this in it:

10,20,30
40,50,60

  1. Always post an example of the format of your data, so we all know
    what you are dealing with.

The format of my data is a .csz file which has been created from an
excel sheet. It has 19 columns and 649 rows. The first row is a column
title (eg: zone_ID, Population, Population0_16… etc) the following
rows are all integer values. So its pretty much a standard excel sheet.

No, no, no. See 1). Posting an example of the format of your data
would look like this:

10, 20, 4

or

3,6,7

  1. Clearly state what the results should be. At this point I still
    don’t have any idea whether you want integers in your matrix or strings.

I would like (within OmniTrans) to read the data in and store it in a
matrix. As a very green ruby programmer (dropped in at the deep end!),
If i was able to achieve this, that would be a major milestone for me.

As far as I can tell OmniTrans is not a ruby gem, nor is it is easy to
locate a download, so I think you are on your own for that part of it.

  1. State what you intend to do with the result, so that people will have
    a better idea of what you really need rather that what you say you
    want.

My longer term objective is to do some fixes on this data. For example.
I may want to say "look at cell (15,1), if this cell is greater than
(15,5) then change this cell to the value of (15,15) - that sort of
thing. An even longer term objective is to take the ‘fixed’ dataset and
link it via ODBC to a data warehouse (this is way off as you can
imagine)

Ok. The code I posted shows you how to get the data from a file and
turn it into an integer; it’s up to you to put it in Omni Trans the way
you want. Once again, to make things easier on yourself start with a
file that contains 2 lines of data, and then you should get be able to
get something working.

Good Points!!! (hope my explanation makes sense!)

  1. Start off writing a program that works on a small amount of data,
    e.g. a file with this in it:

10,20,30
40,50,60

  1. Always post an example of the format of your data, so we all know
    what you are dealing with.

The format of my data is a .csz file which has been created from an
excel sheet. It has 19 columns and 649 rows. The first row is a column
title (eg: zone_ID, Population, Population0_16… etc) the following
rows are all integer values. So its pretty much a standard excel sheet.

  1. Clearly state what the results should be. At this point I still
    don’t have any idea whether you want integers in your matrix or strings.

I would like (within OmniTrans) to read the data in and store it in a
matrix. As a very green ruby programmer (dropped in at the deep end!),
If i was able to achieve this, that would be a major milestone for me.

  1. State what you intend to do with the result, so that people will have
    a better idea of what you really need rather that what you say you
    want.

My longer term objective is to do some fixes on this data. For example.
I may want to say "look at cell (15,1), if this cell is greater than
(15,5) then change this cell to the value of (15,15) - that sort of
thing. An even longer term objective is to take the ‘fixed’ dataset and
link it via ODBC to a data warehouse (this is way off as you can
imagine)

I hope this helps explain myself and i really appreciate all the help
from the listers!!! If i ever get to grips with ruby I will certainly be
answering queries!!

Cheers,

  • John

Hi Thanks for this. Have tried with with the following file called
RubyTest.txt
1,2,3
4,5,6
7,8,9

so the code is as follows:

M:\John N\RubyTest.txt
results = []
ARGF.each do |record|
results << record.split(",").map{|x| x.to_i}
end
p results
puts
s=results.size
s.times do |y|
s.times do |x|
print results[y][x],"\t"
end
puts
end

However I get the follwoing error message

Error: c:\0kjk\a1\TAGM-ver4\jobs\JN_Matrix_3.rb:1:syntax error

M:\John N\RubyTest.txt

c:\0kjk\a1\TAGM-ver4\jobs\JN_Matrix_3.rb:3: syntax error
ARGF.each do |record|

John N. wrote:

However I get the follwoing error message

Error: c:\0kjk\a1\TAGM-ver4\jobs\JN_Matrix_3.rb:1:syntax error

M:\John N\RubyTest.txt

Is it your understanding that this line is ruby code:

M:\John N\RubyTest.txt

What do you think it does?

You are supposed to be learning to write programs here–not just copying
and pasting text.

OK fair point!
Tried it like this:

File.open(“M:\John N\RubyTest\Test.txt”) do |file|

results = []

File.each do |record|
results << record.split(",").map{|x| x.to_i}
end
p results
puts
s=results.size
s.times do |y|
s.times do |x|
print results[y][x],"\t"
end
puts
end

Giving me a syntax error in line 18 though (as in the last ‘end’)

which is the same even when i remove it!

From: John N. [mailto:[email protected]]

This seems to work very well thanks but the results are

produced in a long string. Is it possible to create these

figures as a matrix? (resembling something like excel)

Thanks for your help.

i just modified 7studs’s code,

C:\family\ruby>cat test.txt
1,2,3
4,5,6
7,8,9

C:\family\ruby>cat test.rb
results = []
ARGF.each do |record|
results << record.split(",").map{|x| x.to_i}
end
p results
puts
s=results.size
s.times do |y|
s.times do |x|
print results[y][x],"\t"
end
puts
end

C:\family\ruby>ruby test.rb test.txt
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

1 2 3
4 5 6
7 8 9

C:\family\ruby>ruby test.rb
1,2,3
4,5,6,7
8,9
10
^Z
[[1, 2, 3], [4, 5, 6, 7], [8, 9], [10]]

1 2 3 nil
4 5 6 7
8 9 nil nil
10 nil nil nil

C:\family\ruby>

hth -botp

you’re a genius!! and get this - I even figured out another error I had
in there - File.each should of course be file.each

looks like i’m learning after all :wink:

Thanks a million for your help!!!

John N. wrote:

Giving me a syntax error in line 18 though (as in the last ‘end’)

which is the same even when i remove it!

Well, then add an end. :slight_smile:

open(“M:\John N\RubyTest\Test.txt”) do |file|

results = []

File.each do |record|
    results << record.split(",").map{|x| x.to_i}
end

p results
puts

s=results.size
s.times do |y|
  s.times do |x|
    print results[y][x],"\t"
  end
  puts
end

John N. wrote:

and get this - I even figured out another error I had
in there - File.each should of course be file.each

looks like i’m learning after all :wink:

Nice. Well done.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs