Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- Search = Struct.new :contents, :search, :stop, :direction do
- def iterate_search
- setup
- iterate_search_sub search, 0
- end
- private
- def setup
- @contents_list = contents.lines.to_a
- if direction == :right
- @re = /\S{10}$/
- else
- @contents_list.reverse!
- @re = /\A\S{10}/
- end
- end
- def iterate_search_sub(word, start_line)
- @contents_list.each_with_index do |line, i|
- next if i < start_line
- if line.include? word
- disp word, line
- return if line.include? stop
- iterate_search_sub line[@re], i + 1
- return
- end
- end
- puts "<stop: #{stop}> not found"
- end
- def disp(word, line)
- puts "#{word}\t#{line}"
- puts
- end
- end
- if $0 == __FILE__
- data = ARGF.read
- Search.new.tap do |s|
- s.contents = data
- s.search = 'ABCDEFGHIJ'
- s.stop = 'D1D2D3D4D5'
- s.direction = :right
- s.iterate_search
- end
- puts '-' * 4
- Search.new.tap do |s|
- s.contents = data
- s.search = 'C9D1D2D3D4'
- s.stop = 'BCDEFGHIJK'
- s.direction = :left
- s.iterate_search
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement