"Simon Strandgaard" <0bz63fz3m1qt3001 / sneakemail.com> schrieb im
Newsbeitrag news:pan.2003.06.24.17.34.58.162942 / sneakemail.com...
> On Tue, 24 Jun 2003 13:30:06 +0000, Dan wrote:
>
> > I have a file which I want to extract specific lines and put them
together
> > into a new string, for an example this is what the input file is
>
>
> How about this ?

It has the major disadvantage that it blows for large files because it has
to read them in completely.

If you know that lines always alternate you can do

def scan1(io)
  label = nil

  io.each do |line|
    line.chomp!

    case io.lineno % 2
      when 1 # label
        label = line
      when 0 # text
        puts "[#{label}] has the text [#{line}] right below it"
    end
  end
end

File.open("sc.txt", "r") do |io|
  puts "scan1"
  scan1 io
end

if you know the pattern you can do

def scan2(io)
  label = nil

  io.each do |line|
    line.chomp!

    case line
      when /^\s*line\s+\d+\s*$/o # label
        label = line
      else # text
        puts "[#{label}] has the text [#{line}] right below it"
    end
  end
end

File.open("sc.txt", "r") do |io|
  puts "scan2"
  scan2 io
end

    robert