Hi,
Hello,
I'm trying to remove multiple spaces from a string but can't seemed to get it to do what I want without creating a long subroutine.
I have a text file that contains various pieces of data that I want to import. However, there are gaps in between of various sizes. I want to cut them down to 1 space only.
The only way I can see to do this is to create various substituted strings of various lengths to make sure I get them all :
$str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i; $str =~ s/ / /i;
Is there an easier way to do this as I could end up with 48 of these from 2 spaces to 50 spaces?
If I understand you correctly then this will do what you want:
$str =~ tr/ //s;
Or if you want a slower method:
$str =~ s/ +/ /g;
Or if you want to do it the hard way:
$str = join ' ', split ' ', $str;
By the way, the /i option is for a case insensitive match. For example, in the expression
$str =~ s/hF//i;
the regular expression would match the character 'h' or its upper case version 'H' followed by the character 'F' or its lower case version 'f' which would be the same as writing the expression as:
$str =~ s/[hH][Ff]//;
However in your example the match consists of only space characters which do not have lower or upper case versions so the use of the /i option is superfluous.
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>