From: Robert Klemme Date: 2012-08-03T18:05:40+09:00 Subject: Re: Having difficulty processing text files - some techniques? On Fri, Aug 3, 2012 at 10:39 AM, b1_ __ wrote: > The problem I am having is that I need to have the extracted data > associate with hand data. Hand data might be the date of the hand was > played, the number of players in the hand, the blind level. This means I > need isolate each hand, extract the hand data and put it in a hash for > example, then extract stats like VP$IP for each player (a hand-by-hand > approach rather than a file-wide approach). So the output might go into > a text file specific to each player that would look like: > > Player 1 > VP$IP 2P 3P 4P All HandsAtTables > 2012-07-23 3 201 21 225 534 > 2012-07-24 45 10 3 58 1001 > 2012-07-25 5 5 5 15 420 > > Can I do this without storing data in arrays or objects? Unlikely. You likely need a Hash for storage per hand and probably more - depending on your evaluation requirements. Note that you can create classes for holding data pretty easily by using Struct HandData = Struct.new :date_played, :no_players, :blind_level > I've been > thinking I can use another Finite State Machine, only look for the words > "Hand" (start of hand) and "wins" (end of hand), No, a second FSM for parsing only makes things unnecessary complicated. > the problem is I need > some of the data on the line that has the word "Hand" on it, something I > can't do with my code above. I also need to switch between hands when > there are no lines between the word "wins" and the next line which will > have the word "Hands" on it indicating the start of the next hand. I haven't thought through all the details but chances are that you just need more states. Basically you need one state per logical section which you are parsing. You can then use nested case statements, either state first and then line parsing or other way round: case state when :foo case line when /Hole Cards/ ... state = :bar end when :baz case line when /keyword/ ... end ... end Btw. you can save one level of indentation by using File.foreach: File.open name do |f| f.each line |line| ... end end becomes File.foreach name do |line| ... end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/