Ruby forgets local variables during a while loop?

后端 未结 6 807
我在风中等你
我在风中等你 2020-12-09 17:07

I\'m processing a record-based text file: so I\'m looking for a starting string which constitutes the start of a record: there is no end-of-record marker, so I use the start

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 18:04

    you can simply do this :

    message=''
    
    while line=gets do
       if line =~/hello/ then
          # begin a new record 
          p message unless message == ''
          message = String.new(line)
       else
         message << line
      end
    end
    
    # hello this is a record
    # this is also part of the same record
    # hello this is a new record
    # this is still record 2
    # hello this is record 3 etc etc
    

提交回复
热议问题