From: Rob Biedenharn Date: 2012-02-03T02:20:57+09:00 Subject: Re: Reading specific columns on a text file On Feb 2, 2012, at 11:51 AM, Cassio Godinho wrote: > Hello everyone. > > I'm working on script here and I need do read the 1st and the 10th > columns on a file like this > > R357 10011G1 0 0.0 0 0 0.0 381654.8 7993255.6 827.9 1235959 > > As reference I used the code found in this post: > http://www.ruby-forum.com/topic/161462#new > > The difference is on that snippet the user only wanted to read the first > column, so I tried something like this > > file = File.open(" ") > columns = [] > file.each_line do |line| > columns << line.split(" ")[0 , 11] "R357 10011G1 0 0.0 0 0 0.0 381654.8 7993255.6 827.9 1235959".split(" ").values_at(0,10) => ["R357", "1235959"] (the 11th element is at index 10 when you start from 0) Array#[] with [0,11] means start from the element at index 0 and take 11 elements. -Rob > end > > p columns > > Aparently It reads every column from 0 to 11 > > -- > Posted via http://www.ruby-forum.com/. >