On Mon, Nov 10, 2008 at 09:26, Sharan Basappa <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a string that has value delimited by space
> e.g. 1 2 10 8 etc.
>
> I need to add a keyword wherever there is a space.
> I wrote a small code to try this out:
>
> $str = "one two three";
> $str =~ s/\s/x /g;
>
> In this case, I am trying to insert x where there is a space. one two
> three should
> become one x two x three.
> But the above example results in:
> nex twox three (chops off leading o char)
>
> The interesting this is that this happens only in debug mode.
> The regular output is: onex twox three

I don't know what you call "debug mode", so I can't tell you why you
lose your leading character, but, I can tell you that the substitution
is doing exactly what you have told it to.  You are telling it to
replace any white space (not just spaces) characters with "x ".  If
you want to retain the original character you need to say something
like

$str =~ s/(\s)/${1}x /g;

But you probably really want to say

$str =~ s/ / x /g;

because \s will match all white space characters (i.e. tab, line feed,
form feed, carriage return, space, "\x{85}", "\x{2028}, and
"\x{2029}").

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to