From: Ian Whitlock Date: 2007-07-28T23:28:21+09:00 Subject: Re: How to delete array element and add to previous element Morton Goldberg wrote: > On Jul 27, 2007, at 12:55 PM, Morton Goldberg wrote: > > > 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 > Great lesson for a Ruby Newbie! I had to struggle with why recd[2] << row[2] changed data, but once one understands, it is an eye opener. Page 41 of Programming Ruby explains, and yells gotcha at me. Your code turns it into a tool. In a more traditional (less ruby) form: > raw_data = DATA.read > data = [] i = -1 > raw_data.each do |line| > row = line.chomp.split("\t") if not row[0].empty? # new logical line i += 1 data[i] = row > else # continuation data[i][2] << row[2] > end > end > p data Thanks, again. Ian -- Posted via http://www.ruby-forum.com/.