Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3066
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail |
|---|---|
| From | jake kaiden <jakekaiden@yahoo.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: Making a simple parser |
| Date | Sun, 17 Apr 2011 12:01:47 -0500 |
| Organization | Service de news de lacave.net |
| Lines | 88 |
| Message-ID | <04ac3faa3cf24428d075625283df7645@ruby-forum.com> (permalink) |
| References | <87adbac7061a00a72e282c160ed42414@ruby-forum.com> |
| NNTP-Posting-Host | bristol.highgroove.com |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | talisker.lacave.net 1303059724 31693 65.111.164.187 (17 Apr 2011 17:02:04 GMT) |
| X-Complaints-To | abuse@lacave.net |
| NNTP-Posting-Date | Sun, 17 Apr 2011 17:02:04 +0000 (UTC) |
| In-Reply-To | <87adbac7061a00a72e282c160ed42414@ruby-forum.com> |
| X-Received-From | This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway |
| X-Mail-Count | 381750 |
| X-Ml-Name | ruby-talk |
| X-Rubymirror | Yes |
| X-Ruby-Talk | <04ac3faa3cf24428d075625283df7645@ruby-forum.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.ruby:3066 |
Show key headers only | View raw
hi felipe,
well, i don't know how many possible input types you have, but if
they're not too very many, you could try a very different approach:
## for this test i created three files, "input.eng," "input.esp," and
"input.fr," each with a few lines of random text...
class Parser
attr_reader :output
def initialize(inputfile)
@output = [] ## this can of course be changed to what best suits your
purposes
@oktypes = %W[.eng .esp .fr]
self.checkType(inputfile)
end
def checkType(file)
if File.exists?(file)
if ! @oktypes.include?(File.extname(file))
puts "Unrecognized File Type"
else
@oktypes.collect{|type|
case
when file.downcase.include?(type)
self.parseInput(file)
false
end
}
end
else
puts "File Not Found"
end
end
def parseInput(file)
case
when file.downcase.include?(".eng")
self.engParse(file)
when file.downcase.include?(".esp")
self.espParse(file)
when file.downcase.include?(".fr")
self.frParse(file)
end
end
def loadData(inputfile)
@data = []
file = File.open(inputfile, 'r')
file.collect{|line| @data << line.chomp}
file.close
end
## here's where you do whatever parsing you need to, my examples are
dumb... but the important thing is that you end up with @output
def engParse(file)
self.loadData(file)
@data.collect{|line|
@output << line.reverse}
end
def espParse(file)
self.loadData(file)
@data.collect{|line|
@output << line.upcase}
end
def frParse(file)
self.loadData(file)
@data.collect{|line|
@output << line.upcase.reverse}
end
end #class
test = Parser.new("input.esp")
puts test.output
this may be WAY too simple for what you're trying to do, but hey,
maybe not! ;)
- j
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Making a simple parser Felipe Balbi <balbif@gmail.com> - 2011-04-16 14:52 -0500
Re: Making a simple parser 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-16 22:31 -0500
Re: Making a simple parser jake kaiden <jakekaiden@yahoo.com> - 2011-04-17 12:01 -0500
Re: Making a simple parser Felipe Balbi <balbif@gmail.com> - 2011-04-17 15:22 -0500
Re: Making a simple parser jake kaiden <jakekaiden@yahoo.com> - 2011-04-17 21:10 -0500
Re: Making a simple parser Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-18 02:22 -0500
Re: Making a simple parser Felipe Balbi <balbif@gmail.com> - 2011-04-19 16:03 -0500
Re: Making a simple parser 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-18 13:24 -0500
Re: Making a simple parser 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-18 15:22 -0500
csiph-web