Dave Burt
4/13/2006 5:21:00 PM
Greg wrote:
> I tried what you suggested in your latest post to the thread:
>
> contents = IO.read(filename)
> contents.scan(/^(.*?)\|(.*?)\|(.*?)$/mx) do |a,b,c|
> p [a,b,c]
> end
>
> The source file still wasn't parsed as intended. ...
> ... I'm attaching a sample of the source file I am
> attempting to parse. ...
Programming's about abstraction, and you can't make a good abstraction
if you can't see the concrete. To write code to parse a file, it's best
if you can see the file. Here's the approach that's obvious now:
table = IO.read("German.txt").split(/\n{2,}/m).map {|record|
record.split(/\|\n?/m) }
In English, that's "read the file 'German.txt', split it into records
separated by one or more blank lines, and split each record into fields
separated by either a "|" or "|\n"." The result is an array of records,
each of which is an array of three fields.
> I don't expect you to spend time hunting and pecking around this and I do
> appreciate your replies. I'd like to get a better handle on regexes and what
> folks have posted so far have narrowed things down a lot. For some reason
> this task seems to be a doozie...
Regexes are useful, yes, but to use them effectively in Ruby you also
need to be familiar with the ways you can apply them. (Mainly match,
scan, split.)
And, like I said, this is the better problem description; the one you
posted earlier is rather a different problem.
Cheers,
Dave