[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Text File Parsing

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
3 Answers

greg.kujawa

4/13/2006 8:00:00 PM

0

Dave Burt wrote:

> 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) }

That worked perfectly. Looking at what you've put together it's
starting to fall into place now in my head. Regexes are powerful tools,
and I need to practice on them more to get familiar with all of the ins
and outs. The syntax is so terse at first they had me hesitant to delve
into them past basic stuff, but I will reread my 'Mastering Regular
Expressions' book and test out things. Appreciate the expertise!

Dave Burt

4/14/2006 3:34:00 AM

0

gregarican wrote:
> Dave Burt wrote:
>> table = IO.read("German.txt").split(/\n{2,}/m).map {|record|
>> record.split(/\|\n?/m) }
>
> That worked perfectly. Looking at what you've put together it's
> starting to fall into place now in my head. Regexes are powerful tools,
> and I need to practice on them more to get familiar with all of the ins
> and outs. The syntax is so terse at first they had me hesitant to delve
> into them past basic stuff, but I will reread my 'Mastering Regular
> Expressions' book and test out things. Appreciate the expertise!

Here's another tip: use the /x modifier, and put spaces in your regular
expressions to make them legible.

Is the following easier to read?

table = IO.read("German.txt").split(/ \n {2, } /mx).map do |record|
record.split(/
\| # a | character
\n ? # followed by an optional newline
# (so that the third field doesn't begin with \n)
/mx)
end

Cheers,
Dave

greg.kujawa

4/15/2006 12:40:00 AM

0

Dave Burt wrote:

> Here's another tip: use the /x modifier, and put spaces in your regular
> expressions to make them legible.
>
> Is the following easier to read?
>
>
> table = IO.read("German.txt").split(/ \n {2, } /mx).map do |record|
> record.split(/
> \| # a | character
> \n ? # followed by an optional newline
> # (so that the third field doesn't begin with \n)
> /mx)
> end
>
>
> Cheers,
> Dave


Thanks again for all of the help. This is one of the strongest points I
think Ruby has compared to the other languages. Its community. Everyone
is very helpful, patient, and rarely rude. For technical newsgroups
this is a refreshing rarity. I know as a technical person I myself have
to catch myself being curt with someone at times providing support in
the workplace. But the Rubyists who participate in the NG are extremely
helpful and make what the language has to offer that much stronger.