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?

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. -------------------------------------------------

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.

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....

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: