String parsing?

Hi,

I am new to ruby but have an okay background in perl. I am trying to parse
a string into an array.

string = (foo:0.05,bar:0.115);

I would like the array to look like this:

Array[0] = (
Array[1] = foo
Array[2] = 0.05
Array[3] = ,
Array[4] = bar
Array[5] = 0.115
Array[6] = )
Array[7] = ;

I have a rather clunky perl script for this, but in learning ruby I thought
I might see if there is an elegant way to parse this string using tokens.
Thank you for any help you might have.

-M

Hi,

I am new to ruby but have an okay background in perl. I am trying to parse
a string into an array.

string = (foo:0.05,bar:0.115);

I would like the array to look like this:

Array[0] = (
Array[1] = foo
Array[2] = 0.05
Array[3] = ,
Array[4] = bar
Array[5] = 0.115
Array[6] = )
Array[7] = ;

See if this helps:

>> "(foo:0.05,bar:0.115);".scan(/[a-z]+|\d+\.\d+|[(,);]/)
=> ["(", "foo", "0.05", ",", "bar", "0.115", ")", ";"]

James Edward Gray II

···

On Oct 25, 2005, at 2:32 PM, Michael Zanis wrote:

This may be an overkill, but take a look at my grammar project:

http://rubyforge.org/projects/grammar/

You can build a lexer (what you want) and/or a parser with ruby
syntax that looks kind of like BNF.

But keep in mind the above is only the first release. I'm
building a ruby parser/lexer right now and doing this is
revealing various holes and inconsistencies in the API. The
basics of the API will remain unchanged though: "+" create a
sequence (from 2 grammars) and "|" will create an alternation
(from 2 grammars). The basic idea is that you start with base
grammar objects and you build more complex grammar objects
using various methods to combine or morph them.

···

--- Michael Zanis <mzanis@purdue.edu> wrote:

Hi,

I am new to ruby but have an okay background in perl. I am
trying to parse
a string into an array.

string = (foo:0.05,bar:0.115);

I would like the array to look like this:

Array[0] = (
Array[1] = foo
Array[2] = 0.05
Array[3] = ,
Array[4] = bar
Array[5] = 0.115
Array[6] = )
Array[7] = ;

I have a rather clunky perl script for this, but in learning
ruby I thought
I might see if there is an elegant way to parse this string
using tokens.
Thank you for any help you might have.

-M

__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005