From: Robert Klemme Date: 2008-06-18T15:33:52+09:00 Subject: Re: parsing text into usablle numerical data On 17.06.2008 23:34, Jesús Gabriel y Galán wrote: > 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? I believe he wants to use the first number as index and the rest of the line as array of integers thus yielding a two dimensional array. This is probably what I'd do: data = [] File.foreach "data.txt" do |line| idx, *dat = line.scan(/\d+/).map {|s| s.to_i} data[idx] = dat end Kind regards robert