On Wed, 27 Jun 2001, [iso-8859-1] Stéphane JEAN BAPTISTE wrote:

> 
> Hi.
> I want to replace the String  %0A by nothing. I'm using this line:
>    $description=~tr/%0A//;
> 
> But nothing change.
> 
> What is my problem ?

There's a few things wrong with this, first, you're trying to replace a
string rather than characters with tr///.

To replace a string your best choice is the s/// operator, for your
example you probably want:

  $description =~ s/%0A//g;

for() makes this easy for multiple strings:

  for ($description, $name, $address) {
    s/%0A//g;
  }

It isn't such a saving here, but consider the savings for a more complex
substitution.

The other problem with tr/%0A// was that if you don't supply a replacement
list, tr/// will use the search list, so that:

  $description =~ tr/%0A//;

will not modify $description at all.

If you want those characters to be deleted you need to use the d option:

  $description =~ tr/%0A//d; # remove all %, 0 and A from $description
                             # but don't use such an obvious comment in
                             # your code :)

Now you might be wondering where having the replacement list being the
same as the search list would be useful.  The most obvious example is to
count characters:

  $count = $description =~ tr/ //; # how many spaces?

or you can combine adjacent characters with the s option:

  $description =~ tr/ //s;

I tend to think tr/// is under-used, but this isn't a case where it's
useful.

For information on the tr/// and s/// operators see perldoc perlop under
"Quote and Quote-like Operators

Tony

Reply via email to