CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Removing white spaces at the end of a string

 



tantalum
Novice

Feb 11, 2007, 8:41 AM

Post #1 of 17 (13746 views)
Removing white spaces at the end of a string Can't Post

Please bear with me at the script I'm writing is my first perl script other than the "Hello, world" script.
I'm trying to parse a csv file and in a couple of the fields there is a string value that is padded with blanks so that they are all the same length. I need to get rid of the padding at the end of the strings but there could be white spaces within the string, for example "Jane Doe ", I need just the "Jane Doe" part. What I tried was very simple.
[perl] if($name=~/(.+)\s*$/) $name = $1 [/perl] but as I expected it doesnt work.... Any one have a solution (I know there is a solution but what do you think is the best one)? Is there a chomp like function that will get rid the paddign at the begining/end of a string?


ProBulletin
Novice

Feb 11, 2007, 11:05 AM

Post #2 of 17 (13744 views)
Re: [tantalum] Removing white spaces at the end of a string [In reply to] Can't Post


Quote
Is there a chomp like function


Yes, chomp Wink


Code
local $/ = " "; 
my $string = "Hello ";
print chomp($string);


http://perldoc.perl.org/functions/chomp.html


Regards,
Paul Wilson
ProBulletin Board: http://www.probulletin.com/


KevinR
Veteran


Feb 11, 2007, 11:06 AM

Post #3 of 17 (13743 views)
Re: [tantalum] Removing white spaces at the end of a string [In reply to] Can't Post

you're actually pretty close to getting it right:


Code
$name = 'Jane Doe   '; 
$name = s/\s+$//;


to remove leading spaces:


Code
$name = '  Jane Doe'; 
$name = s/^\s+//;




btw, use the code tags, not the perl tags, which don't work. Unsure
-------------------------------------------------


KevinR
Veteran


Feb 11, 2007, 11:13 AM

Post #4 of 17 (13740 views)
Re: [ProBulletin] Removing white spaces at the end of a string [In reply to] Can't Post


In Reply To

Quote
Is there a chomp like function


Yes, chomp Wink


Code
local $/ = " "; 
my $string = "Hello ";
print chomp($string);


http://perldoc.perl.org/functions/chomp.html



Better using the regexp for this simple task, which will remove as many white spaces as there are on the end without messing around with an important global variable like $/, but it will work for the example you posted.
-------------------------------------------------


tantalum
Novice

Feb 11, 2007, 11:13 AM

Post #5 of 17 (13740 views)
Re: Removing white spaces at the end of a string [In reply to] Can't Post

Thanks every one for the help Wink


ProBulletin
Novice

Feb 11, 2007, 11:23 AM

Post #6 of 17 (13738 views)
Re: [KevinR] Removing white spaces at the end of a string [In reply to] Can't Post

Actually that code before was incorrect. It should be:


Code
chomp($string); 
print $string;



Quote
Better using the regexp for this simple task, which will remove as many white spaces as there are on the end


Yes you're right. I overlooked that there could be multiple spaces.


Regards,
Paul Wilson
ProBulletin Board: http://www.probulletin.com/


KevinR
Veteran


Feb 11, 2007, 11:35 AM

Post #7 of 17 (13736 views)
Re: [ProBulletin] Removing white spaces at the end of a string [In reply to] Can't Post

they could have used chop()


Code
while ($name =~ /\s$/) { 
chop $name;
}


but still, the regexp is the better solution.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Feb 12, 2007, 5:10 AM

Post #8 of 17 (13730 views)
Re: [tantalum] Removing white spaces at the end of a string [In reply to] Can't Post

You've already got the answer to your question, but I thought it was worth pointing out that exactly this question is answered in the Perl FAQ.

How do I strip blank space from the beginning/end of a string?

It's well worth checking the Perl FAQ for answers to your questions. More often than not you'll find a definitive answer there.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


tantalum
Novice

Feb 12, 2007, 6:12 AM

Post #9 of 17 (13729 views)
Re: [davorg] Removing white spaces at the end of a string [In reply to] Can't Post

Aha! the Perl FAQ has been book marked.


tantalum
Novice

Feb 12, 2007, 6:24 AM

Post #10 of 17 (13728 views)
Re: [KevinR] Removing white spaces at the end of a string [In reply to] Can't Post

I tried kevins way but I must be messing something up... here is the code:

Code
sub parse_line{ 
my ($line) = @_ ;
@parts = split ",", $line ;
foreach $n qw(1 2 3 4 6 7){ #get rid of the extra spaces in select columns
$parts[$n] = s/\s+$// ;
}
return @parts ;
}

while(<STDIN>){
chomp $_ ;
@my_line = parse_line $_ ;
print join(',', @my_line), "\n";
}


and I get this out put:

Code
3,,,,,"MD",, 
0,,,,,"Bel Air ",,,"714-978-2200"
1279,,,,,"MD",,
1003,,,,,"LA",,
1004,,,,,"MD",,

it seems to be removing the whole string not just the white spaces....


davorg
Thaumaturge / Moderator

Feb 12, 2007, 6:28 AM

Post #11 of 17 (13727 views)
Re: [tantalum] Removing white spaces at the end of a string [In reply to] Can't Post

Show us the input data.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


tantalum
Novice

Feb 12, 2007, 7:29 AM

Post #12 of 17 (13726 views)
Re: [davorg] Removing white spaces at the end of a string [In reply to] Can't Post


Code
3,"Ks Division 3                 ","123 Main Street               ","                              ","Bel Air         ","MD","21014     ","410-555-1212" 
0,"Kramer-Smilko, Inc. ","123 TEst ","P.O. Box 407 ","Bel Air ","MD","21014 ","714-978-2200"
1279,"CREATIONS BY LAURA ","123 MAIN STREET ","SUITE 404 ","TOWSON ","MD","21016 ","410-555-2424"
1003,"White Radiology ","901 Veterans Blvd. "," ","Metairie ","LA","70005 "," "
1004,"Accounting & Networking Solns ","6600 York Road ","Suite 100a ","Baltimore ","MD","21212 ","301-377-ANSI"



davorg
Thaumaturge / Moderator

Feb 12, 2007, 7:54 AM

Post #13 of 17 (13725 views)
Re: [tantalum] Removing white spaces at the end of a string [In reply to] Can't Post

Ok. Two problems.

First you have:


Code
$parts[$n] = s/\s+$//;


You are carrying out the substitution on the variable $_ and then assigning the results of the substitution (which will be true or false) to $parts[$n]. All of your substitutions fail (as $_ doesn't match your regular expression) and therefore all of the elements of @parts that you work on are assigned an empty string.

This is because you misunderstand how to get s/// to work on a variable. You don't use '=', you use '=~' (the "binding" operator).

So what you actually want is:


Code
$parts[$n] =~ s/\s+$//;


But that doesn't work either. That leaves your data unchanged. So what's the problem?

Well, you asked

"I need to get rid of the padding at the end of the strings"

So we gave you that regular expression. But your white space _isn't_ at the end of your strings. The final character in your string is a double quote ("). As there is no white space at the end of your string, the regular expression doesn't match and no changes are made.

I think that what you really want is this:


Code
$parts[$n] =~ s/\s+"$/"/;


which matches "one or more white space characters followed by a double quote at the end of the string". And then replaces that with just a double quote.

There was no way that we could have seen that problem without seeing your input data as you had described it inaccurately.

Accuracy is vitally important when programming.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


tantalum
Novice

Feb 12, 2007, 7:57 AM

Post #14 of 17 (13724 views)
Re: [davorg] Removing white spaces at the end of a string [In reply to] Can't Post

I figure out the =~ part but it's good to have some one confirm my suspisions. I'm sorry about the mix up, for some reason I though that perl added the "'s and I didnt realize they were part of the string although i should have....


tantalum
Novice

Feb 12, 2007, 8:09 AM

Post #15 of 17 (13722 views)
Re: [davorg] Removing white spaces at the end of a string [In reply to] Can't Post

Ok I revised the program to get rid of the quotes and the white spaces but I use two substitutions and I was wondering how I could combine them into one. Here are the subs.:

Code
$parts[$n]=~s/"//g ; #Take out quotes 
$parts[$n]=~s/\s+$// ; #and trailing white spaces

Thanks every one for bearing with me....


KevinR
Veteran


Feb 12, 2007, 10:37 AM

Post #16 of 17 (13717 views)
Re: [tantalum] Removing white spaces at the end of a string [In reply to] Can't Post

don't combine them into one. Without testing I am pretty sure using two seperate regexp is faster for what you are doing. What might get you some performnce gain is using the tr/// operator instead of s/// for removing the double-quotes:


Code
$parts[$n] =~ tr/"//d ; #Take out quotes  
$parts[$n] =~ s/\s+$// ; #and trailing white spaces

-------------------------------------------------


(This post was edited by KevinR on Feb 12, 2007, 10:40 AM)


tantalum
Novice

Feb 14, 2007, 7:46 AM

Post #17 of 17 (13696 views)
Re: [KevinR] Removing white spaces at the end of a string [In reply to] Can't Post

Thanks.... I wanted to combine them just o see how it would be done.. I was thinking something like s/'|(\s+$)//.... Anu openions

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives