From: "Brian Schröder" Date: 2005-05-10T22:47:39+09:00 Subject: Re: help with reading part of textfile into matrix On 10/05/05, rgilaard@few.vu.nl wrote: > Dear all, > > I am new to Ruby and I'm looking for a way to use Ruby (1.8) to read a part of > a textfile into a matrix. > > The textfile lokks like: > > === Confusion Matrix === > > a b c <-- classified as > 7494 4044 100 | a = WEL > 2039 18691 11 | b = NIET > 707 293 441 | c = NOOIT > > So what i want is that this matrix is read into a matrix variable in a ruby > program which I can use then to manipulate in every way I want. > > Can someone help me with this? > > I figured out that I could maybe use a regexp /\s*a\s*/ to search for the line > with a b c > Then I skip that line and start to read the following line position by position > into the first row of the 3-by-3 matrix. > > How could this be done? > > Thanks in advance, > Robert Gilaard > Confused? ;-) $ cat confusion.rb matrix = [] DATA.each do | line | if /^((?:\s+(?:\d+))+)/ =~ line matrix << $1.split.map{|value| value.to_i} end end p matrix __END__ a b c <-- classified as 7494 4044 100 | a = WEL 2039 18691 11 | b = NIET 707 293 441 | c = NOOIT $ ruby confusion.rb [[7494, 4044, 100], [2039, 18691, 11], [707, 293, 441]] regards, Brian Schr�der -- http://ruby.brian-schroeder.de/ multilingual _non rails_ ruby based vocabulary trainer: http://www.vocabulaire.org/ | http://www.gloser.org/ | http://www.vokabeln.net/