From: stewart@... (Stewart Midwinter) Date: 2002-07-19T07:10:25+09:00 Subject: 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... :-( 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;