user977802
user977802

Reputation: 1

str_replace syntax in perl script

In my below script, the str_replace(rtrim(c_manager),'''','_') doesn't seem to work. I want to replace single quotes with underscores for my arguments. For example:

Input: `S'achin`  
Result: `S_achin`

 $sql = 'select rtrim(f_admin_disabled),'."\n".
       '       convert(varchar,t_password,101),'."\n".
       '       rtrim(c_email),'."\n".
       '       str_replace(rtrim(c_manager),'''','_'),'."\n".
       '       rtrim(c_mgr_email)'."\n".
       '  from tuserprofile'."\n".
       ' where ic_user1 = '."'$user_id'"."\n";

Upvotes: 0

Views: 4997

Answers (3)

Balakumar
Balakumar

Reputation: 650

to replace a character in string

$string=~s/'/_/g;

Syntax

$string=~s/<string>/<replace_string>/g;

Upvotes: 2

ysth
ysth

Reputation: 98398

If you want to have single quotes in a single quoted string to produce str_replace(rtrim(c_manager),'''','_'), you need to either escape them:

'       str_replace(rtrim(c_manager),\'\'\'\',\'_\'),'

or use a different delimiter:

q!       str_replace(rtrim(c_manager),'''','_'),!

Upvotes: 3

Pradeep
Pradeep

Reputation: 3153

Use this subroutine.

sub str_replace {
  my ($s) = @_;

  $s =~ s/'/_/g;

  return $s;
}

Upvotes: 0

Related Questions