Replace with nothing

5

50295

Hi!

I'm trying to replace the whitespace in the following variable $var,
but for some reason its not working.


$var = "whit taker";

$var =~ tr/ //;

print "$var\n"; # still getting "whit taker"


What's wrong? Thanks,

- Olumide
 
I

ioneabu

Hi!

I'm trying to replace the whitespace in the following variable $var,
but for some reason its not working.


$var = "whit taker";

$var =~ tr/ //;

print "$var\n"; # still getting "whit taker"


What's wrong? Thanks,

- Olumide

$var =~ s/ //g; # replace individual space with nothing and do to all
on the line
 
J

Jürgen Exner

I'm trying to replace the whitespace in the following variable $var,
but for some reason its not working.

$var = "whit taker";
$var =~ tr/ //;
print "$var\n"; # still getting "whit taker"

What's wrong? Thanks,

Either you are missing the modifier to delete unreplaced characters (see
"perldoc -f tr" which in turn will point you to "perldoc perlop") or you are
using the wrong operator (maybe you meant "s" instead of "tr").

jue
 
A

Arndt Jonasson

$var =~ s/ //g; # replace individual space with nothing and do to all
on the line

Or if you do want to use tr/// for some reason:

$var =~ tr/ //d;

See perldoc perlop.
 
5

50295

$var =~ s/ //g; # replace individual space with nothing and do to all
on the line

Thanks. Actually, I'm trying to delete all punctuation marks in a
sentence. E.g:

$var = "This, refering to the lady, is my sister.";

So that I have:

This refering to the lady is my sister
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
I'm trying to replace the whitespace in the following variable $var,

(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:
Actually, I'm trying to delete all punctuation marks

You might want to decide on what your question is before you waste
others' time.
E.g:

$var = "This, refering to the lady, is my sister.";

So that I have:

This refering to the lady is my sister

You can easily figure that out.

Try something, if that does not work, ask the *actual* question you
have.

Sinan
 
B

blaine

If you want to remove the punctuation then you can use a regular
expression to do it.

eg:

$var = "This, refering to the lady, is my sister.";
$var =~ s/[,!?\.]//g;
 
G

Gunnar Hjalmarsson

Jürgen Exner said:
Either you are missing the modifier to delete unreplaced characters (see
"perldoc -f tr" which in turn will point you to "perldoc perlop") or you are
using the wrong operator (maybe you meant "s" instead of "tr").

How can tr/// be the wrong operator as long as he is dealing with
characters?
 
G

Greg Bacon

: Either you are missing the modifier to delete unreplaced characters
: (see "perldoc -f tr" which in turn will point you to "perldoc
: perlop") or you are using the wrong operator (maybe you meant "s"
: instead of "tr").

Maybe *you* haven't read the tr/// documentation. :)

Greg
 
T

Tad McClellan

Thanks. Actually, I'm trying to delete all punctuation marks in a
sentence.


So when you asked hundreds of people around the world to help
you, you lied about what problem is to be solved?

Then, after getting a perfectly good answer, you change the question?

Please have more respect for your peers.


tr/a-zA-Z0-9//cd; # strip all punctuation

E.g:

$var = "This, refering to the lady, is my sister.";

So that I have:

This refering to the lady is my sister


Errr, but that still has punctuation in it (spaces).

tr/a-zA-Z0-9 //cd; # strip all punctuation, except spaces
 
T

Tad McClellan

If you want to remove the punctuation then you can use a regular
expression to do it.

eg:

$var = "This, refering to the lady, is my sister.";
$var =~ s/[,!?\.]//g;


$var = "This: has \"punctuation\" too, doesn't it\n";
 
J

John W. Krahn

Tad said:
So when you asked hundreds of people around the world to help
you, you lied about what problem is to be solved?

Then, after getting a perfectly good answer, you change the question?

Please have more respect for your peers.


tr/a-zA-Z0-9//cd; # strip all punctuation

That strips more than just punctuation.

s/[[:punct:]]+//g;


:)

John
 
J

Jürgen Exner

Gunnar said:
How can tr/// be the wrong operator as long as he is dealing with
characters?

If he meant to use s/// then tr/// is not the correct operator because he
wanted to use s///.
If he would have used s/// instead of tr/// then his code would have worked.
Well, at least for the first space in the string.

jue
 
J

Jürgen Exner

Greg said:
Maybe *you* haven't read the tr/// documentation. :)

Ok, you are the second person pointing this out. What am I missing here?
If you want to use tr/// to delete characters, then you have to use the "d"
modifier, right? Or is there any other way with tr///?

jue
 
T

Tad McClellan

Jürgen Exner said:
^^
^^

Ok, you are the second person pointing this out. What am I missing here?


Perhaps the two letters underlined above? :)

If you want to use tr/// to delete characters, then you have to use the "d"
modifier, right?


Right.

But it leaves the UNreplaced characters alone and deletes the replaced chars.
 
A

Arndt Jonasson

Tad McClellan said:
Errr, but that still has punctuation in it (spaces).

tr/a-zA-Z0-9 //cd; # strip all punctuation, except spaces

Do you mean that spaces are punctuation? The C function/macro 'ispunct'
does not agree, and neither does Perl:

% echo "I'm here." | perl -lpe 's/[[:punct:]]//g;'
Im here
%
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top