Re: To replace a string with another

2001-06-27 Thread Tony Cook

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




Re: To replace a string with another

2001-06-27 Thread Stéphane JEAN BAPTISTE



Stéphane JEAN BAPTISTE a écrit :

> 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 ?
>
> tks

PS: There is'nt only 1 string but a lot.





Re: To replace a string with another

2001-06-27 Thread Chas Owens

On 27 Jun 2001 16:11:56 +0200, 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 ?
> 
> tks
> 

tr is not what you want.  tr replaces characters with other characters.
You want s:

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

--
Today is Pungenday, the 32nd day of Confusion in the YOLD 3167
You are what you see.





RE: To replace a string with another

2001-06-27 Thread John Edwards

Try escaping the % symbol.

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


See this link for an explanation.

http://www.netcat.co.uk/rob/perl/win32perltut.html#38-Escaping

-Original Message-
From: Stéphane JEAN BAPTISTE
[mailto:[EMAIL PROTECTED]]
Sent: 27 June 2001 15:12
To: PERL
Subject: To replace a string with another



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 ?

tks



--Confidentiality--.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.