Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and replace ":"

Status
Not open for further replies.

tkjordantk

Programmer
May 19, 2008
5
I've been trying to replace a ":" with "X" using tr. With the code:

if ( index( $test_string, ":" ) == -1) {
$test_string =~ tr/:/X/;
}

I'm sort of new to perl, I was thinking that maybe it was a special character, so I tried to using "\", but that didn't work. Any ideas?

Thanks.
 
The problem is not the regexp, its the "if" condition. Which will only evaluate the expression in the block if : is not in the string. Should be:

Code:
$test_string = 'foo:bar';
if ( index( $test_string, ":" > -1) ) {
  $test_string =~ tr/:/X/;
}
print $test_string;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The 'if' is kind of pointless anyway. If ':' isn't contained in the string, running that 'tr' won't have any adverse effect, as it won't alter it in any way.
 
My conditionals were definitely the problem. I decided to get rid of the them, don't really need them like you said.

Thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top