Using a variable with gsub & sub

In the following statement, I'm replacing , with ,NULL,.

$inrec = $inrec.sub(',\\',',NULL,')

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a
variable with sub & gsub. Can someone help me out?
Thanks.

···

--
Posted via http://www.ruby-forum.com/.

$inrec.gsub! ',\\', ",#{$variable},"

Though I'm not sure why you have the \\ in there, unless it just
didn't end up on the newsgroup correctly.

···

On Nov 15, 3:34 pm, Peter Vanderhaden <bostonanti...@yahoo.com> wrote:

In the following statement, I'm replacing , with ,NULL,.

$inrec = $inrec.sub(',\\',',NULL,')

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a
variable with sub & gsub. Can someone help me out?

The replacement is simply a string, so you could do:

  $inrec = $inrec.sub(',\\',','+$variable+',')

or

  $inrec = $inrec.sub(',\\',',#{$variable},')

···

On Fri, Nov 16, 2007 at 06:34:16AM +0900, Peter Vanderhaden wrote:

In the following statement, I'm replacing , with ,NULL,.

$inrec = $inrec.sub(',\\',',NULL,')

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a
variable with sub & gsub. Can someone help me out?
Thanks.

--
Benjamin A'Lee :: benjamin.alee@subvert.org.uk
Subvert Technologies :: http://subvert.org.uk/

Try this.

",#{var},"

Harry

···

On Nov 16, 2007 6:34 AM, Peter Vanderhaden <bostonantifan@yahoo.com> wrote:

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a
variable with sub & gsub. Can someone help me out?

--
A Look into Japanese Ruby List in English

yermej,
Your solution was exactly what I needed, thanks. As for the \\, that is
part of the first value that is being replaced. The first backslash is
an escape character for the second backshash.
Thanks again,
PETERV

yermej wrote:

···

On Nov 15, 3:34 pm, Peter Vanderhaden <bostonanti...@yahoo.com> wrote:

In the following statement, I'm replacing , with ,NULL,.

$inrec = $inrec.sub(',\\',',NULL,')

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a
variable with sub & gsub. Can someone help me out?

$inrec.gsub! ',\\', ",#{$variable},"

Though I'm not sure why you have the \\ in there, unless it just
didn't end up on the newsgroup correctly.

--
Posted via http://www.ruby-forum.com/\.

Peter Vanderhaden wrote:

yermej,
Your solution was exactly what I needed, thanks. As for the \\, that is part of the first value that is being replaced. The first backslash is an escape character for the second backshash.
Thanks again,
PETERV

yermej wrote:
  

In the following statement, I'm replacing , with ,NULL,.

$inrec = $inrec.sub(',\\',',NULL,')

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a
variable with sub & gsub. Can someone help me out?
      

$inrec.gsub! ',\\', ",#{$variable},"

Though I'm not sure why you have the \\ in there, unless it just
didn't end up on the newsgroup correctly.
    

If you really want to use global variables ($var) then you can also do

$inrec.gsub!(',\\', ",#$variable,")

···

On Nov 15, 3:34 pm, Peter Vanderhaden <bostonanti...@yahoo.com> wrote: