Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Code Parsing
Rodrigo Bermejo
4/25/2008 3:59:00 PM
Dear ruby-list /.
I have a text I file where I need to parse out all Table data-text
similar to the one below.
------------
more code
....
Table MyTable (real a, real b) {
settings="default";
a= 0 {
max= { 0,0,0,0}
mix = { 0,0,0,0}
}
b= 1 {
max= { 0,0,0,0}
mix = { 0,0,0,0}
}
c= 2 {
max= { 0,0,0,0}
mix = { 0,0,0,0}
}
d= 3 {
max= { 0,0,0,0}
mix = { 0,0,0,0}
}
}
....
more code
------
The table code could have different number of elements (a,b,c,d).
I know one approach would be to count the number of brackets found until
you match your 1st bracket.
I suspect there can be many elegant solutions to this problem , so any
charming idea/solution is very welcomed.
-ronnie bermejo.
--
Posted via
http://www.ruby-...
.
2 Answers
Ken Bloom
4/25/2008 5:29:00 PM
0
On Fri, 25 Apr 2008 10:58:49 -0500, Rodrigo Bermejo wrote:
> Dear ruby-list /.
>
> I have a text I file where I need to parse out all Table data-text
> similar to the one below.
> ------------
> more code
> ...
> Table MyTable (real a, real b) {
>
> settings="default";
> a= 0 {
> max= { 0,0,0,0}
> mix = { 0,0,0,0}
> }
> b= 1 {
> max= { 0,0,0,0}
> mix = { 0,0,0,0}
> }
> c= 2 {
> max= { 0,0,0,0}
> mix = { 0,0,0,0}
> }
> d= 3 {
> max= { 0,0,0,0}
> mix = { 0,0,0,0}
> }
> }
>
> ...
> more code
> ------
>
> The table code could have different number of elements (a,b,c,d). I know
> one approach would be to count the number of brackets found until you
> match your 1st bracket.
> I suspect there can be many elegant solutions to this problem , so any
> charming idea/solution is very welcomed.
>
> -ronnie bermejo.
Ruby Quiz #155 Parsing JSON (
http://www.rubyquiz.com/qu...
) dealt
with this. Look there to see whether there are any good ideas there. If
this isn't JSON, then your best bet may be to learn how to use a CFG
parser.
--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...
Rodrigo Bermejo
4/25/2008 11:51:00 PM
0
Ken Bloom wrote:
> On Fri, 25 Apr 2008 10:58:49 -0500, Rodrigo Bermejo wrote:
>
>> a= 0 {
>> }
>> The table code could have different number of elements (a,b,c,d). I know
>> one approach would be to count the number of brackets found until you
>> match your 1st bracket.
>> I suspect there can be many elegant solutions to this problem , so any
>> charming idea/solution is very welcomed.
>>
>> -ronnie bermejo.
>
> Ruby Quiz #155 Parsing JSON (
http://www.rubyquiz.com/qu...
) dealt
> with this. Look there to see whether there are any good ideas there. If
> this isn't JSON, then your best bet may be to learn how to use a CFG
> parser.
Thanks for the pointers Ken.
I found strscan ..so my 1st implementation is:
require 'strscan'
table_file = StringScanner.new( File.read("code") )
while line = table_file.scan_until(/\w+|\W+/)
table=line
if ( line =~ /Table/ ) and
( /\w+.+\(.+\)/ =~ line = table_file.scan_until(/\{/) )
left_brackets=1
right_brackets=0
table << line
temp=""
until left_brackets == right_brackets do
temp = table_file.getbyte
case temp
when "}"
right_brackets+=1
when "{"
left_brackets+=1
end
table << temp
end
puts table
puts "*******"
end
end
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Code Parsing
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password