On Sunday 14 February 2010 02:55:07 pm Zach Bartels wrote: > Start reading a file at a specific line, that is not guaranteed to be > the same line every time.. I.e while the [MATCHES] section in a > Yatta Project file might start on line 300 in one case, in another > where the user has done different tasks (more, or less) there may be > more or less information stored, and the [MATCHES] section might not > be on line 300 every single time.. In other words, you want to start reading once you get to [MATCHES]. > [MATCHES] is the header that proceeds the list of matches for the > frames Yatta is working on. So I assume I need to search the file > and stop at the first occurance of [MATCHES]. Yes. There shouldn't be anything hard about this. Read each line of the file until you find one that matches [MATCHES]. You might look at the each_line method. > Then start reading line by line, one at a time. Each frame is > listed on one line, with one character for the pattern.. So a > parttern of CCCNN would be presented as 5 lines in the text file like > so: > > C > C > C > N > N > > I am supposing I should iterate through each line, and store the > results in an array? Probably. It depend very much what you want to do with them. > If I understand > things right, I don't need to worry about getting the frame count > beforehand and creating a pre-defined matrix with "X number of > spots" to fill.. I can just expand the array on the fly and the > appropriate index number will be given? > > (Array.push?) Yes. > So the question is, how do I open the yatta project file (just a text > file AFAIK) then search until [MATCH] is found - then start > counting on the actual line the pattern data was started on? There > are no blank lines between [MATCH] and the first frame. However > after the last frame in the video there IS a white space, and then > the next section header [POSTPROCESS] occurs. So let me guess -- each header line will be [SOMETHING], right? So you could just do this: open 'filename' do |file| lines = file.each_line line = lines.next.chomp until line == '[MATCH]' line = lines.next.chomp end # Now you're at the MATCH line. array = [] until line =~ /^\s*$/ line = lines.next.chomp array.push line end end I'm sorry, it was way easier to just do that than to try to explain how to do it. > Meanwhile.. storing the patterns to match against.. Maybe I am > overcomplicating it, but I thought about making object instances > (Pattern 1, Pattern 2, etc) that were arrays themselves and then > using a loop to find the pattern the section matches.. I have no idea why you would want to do that. But at least get the above working before you overcomplicate it. What I would do is use a regex to look for anything that looks like [FOO], and store their contents (as arrays) in a hash. Then you would be able to pull out the MATCHES section by doing some_hash['MATCHES']. Even better -- this file looks suspiciously like an INI file. If it is, find a gem to do it for you. > Of course how would you tell Ruby to compare only 5 individual chunks > of the array (0-4, 5-9, 10-14, etc) incrementing by 5 each time so it > doesn't lose its place, backtrack, etc and counts up properly? Now you've lost me. What's special about 5? > I > know how to make an iteration loop to loop through all possible > patterns until a match is found - but not how to specify sections of > an array, much less doing so in a manner that I don't mess up and > compared 0-4, and then 1-5, or skip/miss things in other ways. Look at Array#splice. Better yet, go to ruby-doc.org and read up on the Array documentation. > MasterList: an array holding a list of all items generated from > reading the file. > > Section1: a 5 frame pattern from the file comprised of frames 0 - 4 > Section2: same as above but frames 5 - 10 > ad nauseum Or you could stick them in a giant array and do something like each_slice -- that one is in the Enumerable module, which is included in Array. > I suspect I would use object references throughout as well, and not > necesarrily rely on array indices, to keep things simple since by > breaking it up into multiple arrays you could lose track of the real > frame numbers.. Erm... do you need the real frame numbers? That's easy enough to add. Something like, say you have a variable called 'array' that's a raw list of frames, and you want a list of 5-element frames which each contain the frame number... Try this: array = array.each_with_index.each_slice(5).to_a