Newbie Q: parsing CSV file into 2D array

Sorry about the low level of this Q. I’ve ordered Hal Fulton’s book
but it hasn’t arrived yet… :frowning:

I’m trying to read in a series of records in a CSV file. Each record
has a number of fields. I then want to write out the records in a
paragraph form, if you will. This requires that I create a 2D array.

The following code shows what I am trying to do; it also contains a
sample line from the input file, and a sample desired output file
excerpt.

so far, I am able to read in lines, and write out lines, but I’m not
getting a 2D array, I think. I believe I might need to chomp! each
line into addition to split, but I’m not sure about this. Any ideas?
Thanks!

Please respond to stewart ‘at’ midwinter ‘dot’ ca

format of input file lines:

#Wheel_Name,min_pwr,mid_pwd,max_pwr,fuel_min,fuel_mid,fuel_max,rpm_min,rpm_mid,rpm_max,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,Temp.,Power,
#parsing an input file, and writing reformatted data to output file
#FP-714,2307.741935,2942.370968,3577,9155.6,10712.7,12269.7,10000,12750,15500,-40,3577,-35,3539,-30,3496,-25,3450,-20,3400,-15,3345,-10,3296,-5,3231,0,3171,5,3085,10,2991,15,2886,20,2769,25,2631,30,2504,35,2386,40,2278,
#want to split input lines firstly into array elements: 2d array for
file, 1 array per line
#after array is established, split data into two arrays:

1st array: for data points up to and including max_pwr

2nd array: for data pairs of temp and power

then print out reformatted data into output file, like so:

#FP-107 <–unit name
#23436 27973 32510 <-- min, mid, max
pwr
#61800.92 70410 79018.836 <-- min, mid, max fuel

4270 5337.5 6405 <-- min, mid, max rpm

#-45 32510 32510 32510 <-- a temp, and 3
power values (for 3 rpms)
#-10 32564 32564 32564 <-- ditto
#0 32143 32143 32143
#35 23436 23436 23436
#repeat for next record from input file

ary = []
fh = File.open(“c:\AB_fuel.csv”,“r”);
out = File.open(“c:\AB_fuel.out”,“w”);
fh.each { |line|
a = line.strip.split(’,’)
ary << a
out.puts line
}

test print-out of one array element

puts ary[2]
fh.close; out.close;

[...]
I'm trying to read in a series of records in a CSV file. Each record
has a number of fields. I then want to write out the records in a
paragraph form, if you will. This requires that I create a 2D array.
[...]

Hi.

Why do you need a 2d array? Do you need to refer to a previous row or
anything like that? If not, I think that processing every line and then
throwing it would be the best...

Ok, here are some random thoughts about your problem...

First, you can use a hash to store the values up to rpm_max, so you don't
have to remember array positions. You can use the first line to extract the
labels:

  fh = File.open("your_file.csv", "r")
  out = File.open("your_output", "w")
  
  labels = (fh.gets.chomp.split ',')[0..9]
  
  # now you read the values
  
  fh.each { |line|
    ary = line.chomp.split ','
    
    # the first ten go to a hash
    values = {}
    ary.slice!(0..9).each_with_index { |v, i|
      values[labels[i]] = v
    }
    
    # with the rest we form temp/power pairs... maybe
    # in another hash?
    # first we can transform the strings into numbers, so
    # it would be easier to sort the temps later
    ary.map! { |v| v.to_i }
    # and later we fill the hash
    temps = Hash[*ary]
    
    # print anything using values and temps...
  }
  
  fh.close; out.close

Hope this helps. Hope this compiles, too...