From: 7stud -- Date: 2007-10-16T03:14:00+09:00 Subject: Re: big array, problems with deleting all empty items Simon Schuster wrote: > it seems that I have to run it a couple different times for it to > work.. array.length = 16576, if that could be a potential problem. if > it is, how do I work around it? > > 077:0> array = File.read("/text.txt").to_a.each { |x| x.chomp! } > 080:0> array.each_with_index { |x,y| > array.delete_at(y) if x.empty? == true > } > > still leaves me with plenty of empty array items > 080:0> array[-3] > "" Try running this code: arr = [10, 20, 30, 40] arr.each_with_index do |elmt, i| puts elmt arr.delete_at(i) if elmt == 20 end As you can see that code tries to display every element in the array. Does it succeed? This convoluted code: > array = File.read("/text.txt").to_a.each { |x| x.chomp! } > array.each_with_index { |x,y| > array.delete_at(y) if x.empty? == true > } can be done more directly doing something like this: arr = [] IO.foreach("data.txt") do |line| if line != "\n" arr << line.chomp end end puts arr -- Posted via http://www.ruby-forum.com/.