Erik Lewis wrote:

I have to changes all the spaces in a string to +'s. Is there an easy way to do this. The length of the string and the number of spaces will always be changing.

You are probably looking for tr, see perldoc -f tr.

Do you mean chr(32) only, or also other types of space?


To for example replace space and tab runs by a single plus, try this:

    s{ [[:blank:]]+ } {+}xg;

which is equivalent to

    tr { \t} {+}s;


Example:

perl -wle '

    my $var = "abc def  \t g";

    (my $v1 = $var ) =~ s{ [[:blank:]]+ } {+}xg;

    (my $v2 = $var ) =~ tr { \t} {+}s;

    print "<$_>" for $var, $v1, $v2;
'
<abc def      g>
<abc+def+g>
<abc+def+g>

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to