From: Alpha Blue Date: 2009-12-03T04:15:15+09:00 Subject: Need help with writefile line ranges within extensive file Hi Everyone, I have the following file that I started to work on: class EuiiiReader def initialize(writefile,filename) @writefile = writefile @filename = filename @provnum = /^\d{1,4}\=\{/ end def readfile File.open(@filename, "r") do |infile| @linenumarray = [] while (line = infile.gets) if (line =~ @provnum) linenum = $. @linenumarray += [linenum] end end end end def writefile open(@writefile, "w") { |f| f.puts "Verifying File..." f.puts "=================" File.open(@filename, "r") do |infile| while (line = infile.gets) # Need help here # @linenumarray contains all of the starting file lines # I need to output the lines + 8 lines after each one listed in the array to an output file f.puts "Something to do here" end end } end end k = EuiiiReader.new('compare.txt', 'Italy_alternate.eu3') k.readfile k.writefile ============= Here is my issue. I'm still trying to get better with utilizing Ruby in various situations. Since I read a lot of files and love file readers, I thought I would concentrate on this. I have a simple saved game file from Europa Universallis III which contains approximately 850,000 lines. The saved game file is currently corrupt and I believe I know the issue and need to have a more permanent fix in place for if it happens again. The file will look something similar in certain segments: .... 1={ flags={ } variables={ } name="Stockholm" owner="SCA" controller="SCA" core="SWE" core="SCA" culture=swedish .... My class above starts with reading the entire file and locating province number lines for 1={, 2={, etc. etc. to say 1894={. When it finds these lines, it stores the line numbers into an array called @linenumarray. This is done in the readfile method. In the writefile method, I need to output to file the line and the next 8 lines after it for every line number stored in the array. So, the file would be written to: Verifying File... ================= 1={ flags={ } variables={ } name="Stockholm" owner="SCA" controller="SCA" core="SWE" 2={ flags={ } variables={ } name="Stockholm" owner="SCA" controller="SCA" core="SWE" etc.... My problem is I don't know how to pull the line number from the array and attach another 8 lines to it. The commented section in the writefile method is where the placement should occur. Again, as this is a learning process for me, whatever best practices should be used instead of what I've written would also be greatly appreciated. I want to make sure I simplify and expand upon my knowledge and not do things incorrectly. As ruby is a wonderful language with many ways of achieving an end result, I might be taking a more sloppy approach and for that I apologize. Any help would be greatly appreciated. Thanks, -- Posted via http://www.ruby-forum.com/.