1

I have many text documents that are filled with sudo tables like the example below. What would be the best way to parse out the table into something like a hash or array?

Do I write a custom algorithm or are there existing libraries out there?

PLACE  NO.  NAME                    DIV    RANK  SWIM  TRANS RANK  BIKE   MPH   TRANS RANK   RUN   PACE  TIME    
===== ===== ======================= ===    ====  ====  ===== ==== ======= ===== ===== ====  ====== ===== ======= 
    1     1 Krige Schabort          PCHAL    3   22:40  1:08   25 1:14:15 20.2  2:25    1   26:24  4:24 2:06:49                
    2    12 Kevin Moats             M5559   11   24:41  0:46    1 1:06:01 22.7  0:44    9   42:50  7:09 2:15:00                
jspooner
  • 10,975
  • 11
  • 58
  • 81
  • Seems like fixed-width flat files to me. This stuff has been around since computers were built. See here for recommendations: http://stackoverflow.com/questions/1609807/whats-the-best-way-of-parsing-a-fixed-width-formatted-file-in-java – Candide Sep 20 '11 at 02:35
  • Is there a reason you have added the tag `ruby` to your question? If you would like to have a ruby answer, you should state that in the question. I had good results with ruby on windows, because the other options meant that I had to install something like Cygwin, and ruby was in place. – mliebelt Sep 20 '11 at 05:34

2 Answers2

3

You might want to try slither

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
1

if the format is length-fixed and filled with space within, what about String#unpack

you may see http://ruby-doc.org/core/classes/String.html#M001112 for details

Jokester
  • 5,501
  • 3
  • 31
  • 39
  • I like the idea of unpack but I haven't been able to figure out a unpacking format that works. – jspooner Sep 20 '11 at 07:09
  • @jspooner actually the `perlpacktut` page of perl gives an example of extracting fixed-width fields out of string, and I have not been using ruby's pack a lot. they seem to be quite alike though.. http://perldoc.perl.org/perlpacktut.html#Packing-Text – Jokester Sep 20 '11 at 16:28