From: kjana@...4lab.to (YANAGAWA Kazuhisa) Date: 2002-05-06T12:05:02+09:00 Subject: Re: newbie Q: how to strip blank lines from file? In message stewart@NOWHERE_midwinter.ca writes: > # IO.foreach("snip.txt") { |line| puts line } You can skip a blank line to eliminate it from output so.... IO.foreach("snip.txt") { |line| next if line =~ /^\s*$/; puts line } here, `next' for forwarding to the next iteration. > arr = IO.readlines("snip.txt") > puts "I read in " + arr.length.to_s + "lines from file." > def blank (s) # looking for blank lines to chop > (s =~ /\S/) != nil > end > i = 0 > while i < arr.length do > if blank(arr[i]) == nil then arr[i] = nil > IO.printf("snip.out") > end > # file.close("snip.out") > end If you prefer reading the whole file into an Array, then you can use Enumerable#reject (or reject! for in-place modification) for deleting blank lines like: arr.reject { |line| line =~ /^\s*$/ } Actually File < IO includes Enumerable, so you can.... puts File.open("snip.txt").reject { |line| line !~ /\S/ } # but this doesn't close the File. -- kjana@dm4lab.to May 6, 2002 The more haste, the less speed.