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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whitespace questioni 1

Status
Not open for further replies.

nawlej

Programmer
Mar 26, 2004
380
US
I am trying to filter out all excess space in between a string. It looks something like this:

value1 value2 value3

Would the correct search and replace be:

s/\s+// #this is the one I think it might be

or:

s/\s(.*)//

or someting else?

Bear in mine, I am running these trough a split delimited by a single space after this. Can anyone reaffirm me on this?
 
I think what you want is
s/\s+/\s/g
If you want the string to be single space delimited after the substitution.

Of course you may be able to save a step by using this for the split
split/\s+/, $string_to_split
 
Ok, now it wont let me replace my whitespace with a \s, which kind of makes sense to me....so I replaced that with just plain / /. We will see hwo it works.
 
Code:
$tmp="a    d c b  f ";
$tmp=~s\s+/ /g;

Should do it

--Paul
 
I just made it easy on my self and split on shitespace. The other way worked too. I actually tried it that way a few minutes ago. Boy I will sure be glad when I can do things with perl I used to be able to do. Thanks.
 
Code:
$tmp=~s/\s+/ /g;
$tmp=~s\s+/ /g;
Testing to see if it was a typo above or if the code wrap is dropping slashes

--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top