Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

space elimination 1

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
How do I eliminate spaces using PERL??.

say , I have
$_ = "bill gates "
or $_ "bill michael gates".

I want all the spaces eliminated and the o/p as
"billgates" or "billmichaelgates"

Thanks in advance.
 
These should work

$_ =~ s/\ //;

or for all whitespace

$_ =~ s/[\ \t\n]+//;

Miah
 
There are a number of things wrong with the above examples:

1. If you are using $_ you don't need to specify it.

2. You don't need to escape the space in the pattern.

3. You should be using the '+' flag for "one or more" after the space, it's more efficient.

4. You should be using the 'g' flag for "global", otherwise it will only remove the FIRST space.

5. What's wrong with using the whitespace pattern '\s'?

To remove just spaces (from $_) use:
Code:
s/ +//g;
To remove all whitespace (from $_) use:
Code:
s/\s+//g;
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top