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

Similar Threads

gsub ? 2
gsub and backslashes 15
gsub help 7
gsub wrapper 4
Interpolating $1, etc., from within a gsub block 4
gsub wrapper re-submitted 0
gsub pattern substitution and ${...} 7
Ruby GSUB question 6

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top