simple gsub question \' \` what?

D

Dustin Anderson

Sorry for the simple question, I just can't figure this out after a
couple hours...

I need to parse a string:

string = column-1:block-0,block-2,block-1,block-3

and I need to extract everything before the : (colon) into one variable
(should end up like this:
string1 = column-1

and then the rest of the string (block-0,block-2,block-1,block3) into an
array...

How can I use gsub to extract everything before the colon?

I've looked at the documentation and just can't understand how to use
the \` or \' syntax... I want the two new strings to be:

string1 = column-1
string2 = ["block-0", "block-2", "block-1", "block-3"]

I'm thinking it's something like this:

string.gsub(/:/, "\`")

really, I just don't get it...

the documentation that I'm looking at is here:
http://dev.rubycentral.com/ref/ref_c_string.html

thanks a bunch!
-Dustin
 
S

Stefano Crocco

Alle venerd=C3=AC 30 marzo 2007, Dustin Anderson ha scritto:
Sorry for the simple question, I just can't figure this out after a
couple hours...
=20
I need to parse a string:
=20
string =3D column-1:block-0,block-2,block-1,block-3
=20
and I need to extract everything before the : (colon) into one variable
(should end up like this:
string1 =3D column-1
=20
and then the rest of the string (block-0,block-2,block-1,block3) into an
array...
=20
How can I use gsub to extract everything before the colon?
=20
I've looked at the documentation and just can't understand how to use
the \` or \' syntax... I want the two new strings to be:
=20
string1 =3D column-1
string2 =3D ["block-0", "block-2", "block-1", "block-3"]
=20
I'm thinking it's something like this:
=20
string.gsub(/:/, "\`")
=20
really, I just don't get it...
=20
the documentation that I'm looking at is here:
http://dev.rubycentral.com/ref/ref_c_string.html
=20
thanks a bunch!
-Dustin
=20

I don't think you need gsub here. gsub is used to replace parts of a string=
=20
with other characters. What you need is a regexp. This should work:

string.match( /^([^:]*):(.*)$/ )
string1=3D$1
string2=3D$2.split(',')

The regexp will put anything from the beginning of the string until the=20
first : (excluded) in the global variable $1 and anything after the : in th=
e=20
variable $2. You can then convert $2 to an array of string using the=20
String#split method.

I hope this helps

Stefano
 
T

Timothy Hunter

Dustin said:
Sorry for the simple question, I just can't figure this out after a
couple hours...

I need to parse a string:

string = column-1:block-0,block-2,block-1,block-3

and I need to extract everything before the : (colon) into one variable
(should end up like this:
string1 = column-1

and then the rest of the string (block-0,block-2,block-1,block3) into an
array...
I'd use two calls to split:

irb(main):001:0> string = 'column-1:block-0,block-2,block-1,block-3'
=> "column-1:block-0,block-2,block-1,block-3"
irb(main):002:0> string1, s = string.split(':')
=> ["column-1", "block-0,block-2,block-1,block-3"]
irb(main):003:0> string1
=> "column-1"
irb(main):004:0> string2 = s.split(',')
=> ["block-0", "block-2", "block-1", "block-3"]
irb(main):005:0>

You could probably turn that into a one-liner but that's just golfing.
 
R

Rob Biedenharn

Dustin said:
Sorry for the simple question, I just can't figure this out after a
couple hours...

I need to parse a string:

string = column-1:block-0,block-2,block-1,block-3

and I need to extract everything before the : (colon) into one
variable
(should end up like this:
string1 = column-1

and then the rest of the string (block-0,block-2,block-1,block3)
into an
array...
I'd use two calls to split:

irb(main):001:0> string = 'column-1:block-0,block-2,block-1,block-3'
=> "column-1:block-0,block-2,block-1,block-3"
irb(main):002:0> string1, s = string.split(':')
=> ["column-1", "block-0,block-2,block-1,block-3"]
irb(main):003:0> string1
=> "column-1"
irb(main):004:0> string2 = s.split(',')
=> ["block-0", "block-2", "block-1", "block-3"]
irb(main):005:0>

You could probably turn that into a one-liner but that's just golfing.

Here's a bit of "live" code that does the same thing (plus .to_f on
the elements before turning them into a Vector, but you can knock
that part out yourself)

def add line
id, vals = line.split(/:\s*/, 2)
@labels << id
@data << Vector[*(vals.split(',').map {|v| v.to_f})]
end

Note that the second arg (2) to split is important if a ':' can occur
anywhere in your block-N parts. Of course, you just need vals.split
(',')

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top