From: "Jesús Gabriel y Galán" Date: 2008-06-18T06:34:29+09:00 Subject: Re: parsing text into usablle numerical data On Tue, Jun 17, 2008 at 7:44 PM, Cthulhu __ wrote: > Hey total ruby n00b here... > I'm having trouble with parsing data into ruby for statistical analysis. > > The data looks like this: > 32 0 0 0 0 0 0 0 0 8412803500 0 0 0 0 0 0 0 46655166 0 0 0 0 0 0 0 0 0 0 > 0 0 0 0 0 240554000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 85321000 0 0 > 0 0 0 0 0 479719000 0 0 0 97823285 283432000 0 73887750 0 0 157225000 > 88659750 285211000 70285000 0 161747000 161167000 234739666 120400000 > 300083000 0 0 202327250 111865000 183127000 0 161027000 0 0 0 > > 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > > I need to store the index of the entry, in this case 32 and 33, as the > name of the 2 Dimensional array (array of arrays as ruby handles it?) > and then each value (alot of zeros in the case of 33) as a unique entry. If you need a "name" for the entry then you might be thinking of a hash, in which 32 and 33 would be the keys and the rest of the line the value. What do you mean by "a unique entry"? Maybe the rest of the string, as a string? An array with an element for each number in the string? > The format is plain tyext right now and I have had no luck using > File.readline > > For some reason this does NOT work, the array dimensions do not match > expected structure: > elsif /aqua_t/=~(files_to_parse[i]) > line_counter = 0 > line = File.readlines(files_to_parse[i]).each do |line| The each method returns the full enumerable, so after the iteration, the line variable will reference an array of all lines. Not sure that's what you want. > line.each{|x| x.to_i} This line does nothing, you don't assign the return values of the block anywhere or modify anything inside the block. But this line makes me think you want an array of numbers. > raw_data_t[line_counter]=line.split > aqua = [0] > line_counter+=1 > end#ends block over lines This works for me: irb(main):001:0> line_counter = 0 irb(main):005:0> raw = [] irb(main):006:0> File.readlines("data.txt").each do |line| irb(main):007:1* raw[line_counter] = line.split irb(main):008:1> line_counter += 1 irb(main):009:1> end irb(main):010:0> raw => [["32", "0", "0", "0", "0", "0", "0", "0", "0", "8412803500", "0", "0", "0", "0", "0", "0", "0", "46655166", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "240554000", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "85321000", "0", "0", "0", "0", "0", "0", "0", "479719000", "0", "0", "0", "97823285", "283432000", "0", "73887750", "0", "0", "157225000", "88659750", "285211000", "70285000", "0", "161747000", "161167000", "234739666", "120400000", "300083000", "0", "0", "202327250", "111865000", "183127000", "0", "161027000", "0", "0", "0"], ["33", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]] So I'm not sure what the exact problem you are having is. The above could be simplified, though, to avoid the counter: raw = [] File.readlines("data.txt").each do |line| raw << line.split end If you need the hash with keys 32 and 33, and as value an array of numbers, you can do this: raw = {} File.readlines("data.txt").each do |line| key, *value = line.split value.map! {|x| x.to_i} raw[key] = value end If the key needs to be a number you can to_i it too. Hope this helps, Jesus.