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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a string function???!!

Status
Not open for further replies.

karyn1617

Programmer
Feb 17, 2005
6
US
I am trying to find every occurrence of a pattern within a string, and remove it. For example:

$string = "abcdabcdabcd";

and I want to remove every a so that $string = "bcdbcdbcd"

is there a function that does that? Or a simple way. I know I can use index() and loop through and do substr() etc...but is there a more eloquent solution?
 
you can use either tr/// or s///
see perldoc perlop for details

$string =~ s/a//g;
 
use tr/// if you just want to remove the a's

$string =~ tr/a//d;

just a good programming habit to use tr/// whenever possible instead of s///
 
depends on how fancy your pattern is, does 'abcdabcd' become 'bcdbcd' because all a's are removed, or is it only a's that come before b's, or a's that are not followed by an r, or every fourth alphanumeric after the first, including the first, or any occurance of the first character..... Describe a pattern, perl can munge it.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top