From: Morton Goldberg Date: 2007-07-28T08:22:53+09:00 Subject: Re: How to delete array element and add to previous element On Jul 27, 2007, at 12:55 PM, Morton Goldberg wrote: > The following code isn't a full solution to your problem, but I > think it might suggest a strategy you can use. > > > raw_data = DATA.read > data = [] > raw_data.each do |line| > data << line.chomp.split(/\t/) > end > recd = nil > data.each_with_index do |row, i| > if row[0].empty? > recd[2] << row[2] > data[i] = nil > else > recd = row > end > end > data.compact! > p data > > __END__ > 49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, Independent, 5% > RP32,RP33 > 50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5% > 51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W > R37,R39,R41,R42,R43,R49, > R50,R51,R52,R58,R68,R69, > R71,R72,R74,R85,R95,R117, > R129,R130,R155,R156,R158, > R160 > > > > [["49", "7", "RP13,RP15,RP17,RP19,RP24,RP32,RP33", "12X2", "33XD", > "Wireless, Independent, 5%"], > ["50", "4", "RP27,RP28,RP30,RP31 ", "10XC4", "3X567", "Network, > Isolated, 5%"], > ["51", "31", > "R1,R2,R8,R30,R32,R33,R35,R37,R39,R41,R42,R43,R49,R50,R51,R52,R58,R68, > R69,R71,R72,R74,R85,R95,R117,R129,R130,R155,R156,R158,R160", > "0603,R10K,1%", "0603", "3%,1/12W"]] > The following does the job in one pass, rather than three: raw_data = DATA.read data = [] recd = nil raw_data.each do |line| row = line.chomp.split("\t") if row[0].empty? recd[2] << row[2] else data << (recd = row) end end p data Regards, Morton