[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: MATRIX PROBLEMS (AGAIN!

7stud 7stud

10/8/2007 1:46:00 PM

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

7 Answers

7stud 7stud

10/8/2007 2:02:00 PM

0

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

--
Posted via http://www.ruby-....

7stud 7stud

10/8/2007 2:11:00 PM

0

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]


--
Posted via http://www.ruby-....

7stud 7stud

10/8/2007 3:17:00 PM

0

John Nott 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

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

3) 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.

4) 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.




--
Posted via http://www.ruby-....

7stud 7stud

10/8/2007 4:58:00 PM

0

John Nott 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
>
> 2) 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

> 3) 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.


> 4) 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.
--
Posted via http://www.ruby-....

7stud 7stud

10/9/2007 9:52:00 AM

0

John Nott 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.
--
Posted via http://www.ruby-....

7stud 7stud

10/9/2007 10:58:00 AM

0

John Nott 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. :)

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


--
Posted via http://www.ruby-....

7stud 7stud

10/9/2007 11:38:00 AM

0

John Nott 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 ;-)
>

Nice. Well done.
--
Posted via http://www.ruby-....