Difference between tr and s

Reply

Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Difference between tr and s

 
0
  #1
Feb 22nd, 2007
I am working with form processing and text manipulation, and I found two useful functions; tr/// and s/// They do pretty much the same thing, but I was wondering if anyone could explain to me what the difference is and if one would ever be more useful than the other in a situation. Thanks!

- Dano
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Difference between tr and s

 
0
  #2
Feb 24th, 2007
tr/// only replaces characters for characters (transliteration). It is very limited but faster than s/// for simple character replacing. s/// is a full blown regexp that can use all of perls various options for pattern matching and substitution.

For example if all you wanted to do was replace all A's with Z's tr is the better choice:

tr/A/Z/;

tr/// can't even use case insensitive matching so to replace all 'A' and 'a' with 'Z' you have to do this:

tr/aA/Z/;

the only useful option with tr/// is the range operator:

tr/a-z/A-Z/

besides that the three basic modifiers are:

c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.

see:

http://perldoc.perl.org/perlop.html

for more information
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC