From: Yaj Bhattacharya Date: 2009-03-14T03:02:51+09:00 Subject: Re: reading data files with headers and columns Thanks very much Luc, here are a few follow up questions (complete noob) # here's the columns as arrays @column1 = [] # should this be @column1=[variable1] or just the empty square brackets? # open file in read mode (r) file = File.new("") # I am guessing that a space column separator #would be " ", does it work with arbit number of multiple spaces between the columns i.e. if the #column width is variable but between two # columns there are one or several spaces? # assign variable to column @column1 << array[0] # if @column1=[variable1] comment in the second line of the above code was not correct, then #where do the variables get their names? # ... process other columns end On Mar 13, 1:23 pm, Luc Traonmilin wrote: > Yaj Bhattacharya a écrit : > > > > > Hello, > > > As a noob to Ruby, I need to read a data file with the first few lines > > as header, then the rest of the file as columns of different variables > > as arrays (in this case, 5 columns). > > After reading in the variables, I want to calculate some operations > > with each unique data field address (e.g. column3, row4), then write > > out the file with headers and data in columns. > > > Could someone help with a few lines of basic code? > > > Thanks in advance > > Yaj > > You could start with this basic piece of code (replace with your > code): >   # here's the columns as arrays >   @column1 = [] >   >   # open file in read mode (r) >   file = File.new("   # read each line one after another and process it >   file.each_line do |line| >     # process as header if necessary >     # or >     # extract your variables in the line into an array >     array = line.split("") >     # assign variable to column >     @column1 << array[0] >     # ... process other columns >   end > > Additionnally you can google "ruby array" and "ruby file", the API docs > are well documented. > > Luc- Hide quoted text - > > - Show quoted text -