943,808 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 11490
  • Perl RSS
Feb 22nd, 2007
0

Difference between tr and s

Expand Post »
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
Similar Threads
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Feb 24th, 2007
0

Re: Difference between tr and s

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
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Text file formatting by perl
Next Thread in Perl Forum Timeline: How to skip a line !





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC