Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Replacing spaces (http://www.velocityreviews.com/forums/t888109-replacing-spaces.html)

Aristotle 09-19-2004 09:39 AM

Replacing spaces
 
I'm trying to replace spaces at the beggining of a string, with
" " .
Not all spaces by a single   , but rather each space by a single
" "

eg
" make love not war" --> "    make love not
war"
" follow the white rabbit" --> "  follow the white rabbit"

ie " " should replace only the beggining spaces (one by one), but
not other spaces.

The way i'm doing this for now is

$string =~ s/ /\&nbsp\;/g;
$string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;

ie, first replacing all spaces with   , then replacing again
those between two words. It gets the job somewhat done (a bit
inefficiently, since if there are other characters within the string
(like ",.-:;" ) the " " arent being replaced).

If i try to use $string =~ s/^\s+/\&nbsp\;/; then all beggining spaces
are being replaced by a single " ", while what i need is the
number of " " to match the number of spaces at the beggining of
the string.

I'd appreciate your help on this.

Thank you in advance.

Abhinav 09-19-2004 10:28 AM

Re: Replacing spaces
 
Aristotle wrote:
> I'm trying to replace spaces at the beggining of a string, with
> " " .
> Not all spaces by a single   , but rather each space by a single
> " "
>
> eg
> " make love not war" --> "    make love not
> war"
> " follow the white rabbit" --> "  follow the white rabbit"
>
> ie " " should replace only the beggining spaces (one by one), but
> not other spaces.
>
> The way i'm doing this for now is
>
> $string =~ s/ /\&nbsp\;/g;
> $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
>


I'm just learning these kind of things, but you can replace those two with

$string =~ s/^([ ](?{ $cnt .= "\&nbsp\;"}))*/$cnt/;

Worked with the spaces ..

Awaiting critiques from the regulars .. :)


Abhinav

Jürgen Exner 09-19-2004 10:50 AM

Re: Replacing spaces
 
Aristotle wrote:
> I'm trying to replace spaces at the beggining of a string, with
> " " .
> Not all spaces by a single   , but rather each space by a single
> " "
>
> eg
> " make love not war" --> "    make love not
> war"
> " follow the white rabbit" --> "  follow the white rabbit"
>
> ie " " should replace only the beggining spaces (one by one), but
> not other spaces.


One way to do it:

s/^(\s*)/' ' x length $1/e;

jue



Eberhard Niendorf 09-19-2004 11:22 AM

Re: Replacing spaces
 
Aristotle <qwerty97654@hotmail.com> wrote:

> I'm trying to replace spaces at the beggining of a string, with
> "&nbsp;" .
> Not all spaces by a single &nbsp; , but rather each space by a single
> "&nbsp;"
>
> eg
> " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
> war"
> " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
>
> ie "&nbsp;" should replace only the beggining spaces (one by one), but
> not other spaces.
>
> The way i'm doing this for now is
>
> $string =~ s/ /\&nbsp\;/g;
> $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
>


This should work

1 while ( $string =~ s/^\s/\&nbsp;/g );

Eberhard

Jürgen Exner 09-19-2004 11:23 AM

Re: Replacing spaces
 
Eberhard Niendorf wrote:
> Aristotle <qwerty97654@hotmail.com> wrote:
>
>> I'm trying to replace spaces at the beggining of a string, with
>> "&nbsp;" .
>> Not all spaces by a single &nbsp; , but rather each space by a single
>> "&nbsp;"
>>
>> eg
>> " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
>> war"
>> " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
>>
>> ie "&nbsp;" should replace only the beggining spaces (one by one),
>> but not other spaces.


> This should work
>
> 1 while ( $string =~ s/^\s/\&nbsp;/g );


Why didn't you test it?
Your while loop succeeds exactly once, then there is no leading space any
longer and all remaining spaces will, well, remain.

jue



Eberhard Niendorf 09-19-2004 11:35 AM

Re: Replacing spaces
 
Jürgen Exner <jurgenex@hotmail.com> wrote:

>> This should work
>>
>> 1 while ( $string =~ s/^\s/\&nbsp;/g );

>
> Why didn't you test it?
> Your while loop succeeds exactly once, then there is no leading space any
> longer and all remaining spaces will, well, remain.
>
> jue


Sorry, shame on me! You are right I was WRONG, I've wrong tested.

Eberhard

Abhinav 09-19-2004 12:18 PM

Re: Replacing spaces
 
Eberhard Niendorf wrote:
> Aristotle <qwerty97654@hotmail.com> wrote:
>
>
>>I'm trying to replace spaces at the beggining of a string, with
>>"&nbsp;" .
>>Not all spaces by a single &nbsp; , but rather each space by a single
>>"&nbsp;"
>>
>>eg
>>" make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
>>war"
>>" follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
>>
>>ie "&nbsp;" should replace only the beggining spaces (one by one), but
>>not other spaces.
>>
>>The way i'm doing this for now is
>>
>> $string =~ s/ /\&nbsp\;/g;
>> $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
>>

>
>
> This should work
>
> 1 while ( $string =~ s/^\s/\&nbsp;/g );
>


I tried this before brewing that complex concoction up-thread .. It will
not work as the match succeeds only once ..

Of course, Jurgen's solution is better than mine :)

Regards

Abhinav

Anno Siegel 09-20-2004 08:40 AM

Re: Replacing spaces
 
Abhinav <matrix_calling@yahoo.dot.com> wrote in comp.lang.perl.misc:
> Aristotle wrote:
> > I'm trying to replace spaces at the beggining of a string, with
> > "&nbsp;" .
> > Not all spaces by a single &nbsp; , but rather each space by a single
> > "&nbsp;"
> >
> > eg
> > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
> > war"
> > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
> >
> > ie "&nbsp;" should replace only the beggining spaces (one by one), but
> > not other spaces.
> >
> > The way i'm doing this for now is
> >
> > $string =~ s/ /\&nbsp\;/g;
> > $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
> >

>
> I'm just learning these kind of things, but you can replace those two with
>
> $string =~ s/^([ ](?{ $cnt .= "\&nbsp\;"}))*/$cnt/;
>
> Worked with the spaces ..
>
> Awaiting critiques from the regulars .. :)


Well, it doesn't run under strictures. Also, $cnt should be cleared
before each call, otherwise "&nbsp"s would collect in it.

Anno

Anno Siegel 09-20-2004 08:57 AM

Re: Replacing spaces
 
Jürgen Exner <jurgenex@hotmail.com> wrote in comp.lang.perl.misc:
> Aristotle wrote:
> > I'm trying to replace spaces at the beggining of a string, with
> > "&nbsp;" .
> > Not all spaces by a single &nbsp; , but rather each space by a single
> > "&nbsp;"
> >
> > eg
> > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
> > war"
> > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
> >
> > ie "&nbsp;" should replace only the beggining spaces (one by one), but
> > not other spaces.

>
> One way to do it:
>
> s/^(\s*)/'&nbsp;' x length $1/e;


Here's another:

/ */g;
substr( $_, 0, pos) =~ s/ /&nbsp/g;

Anno

Anno Siegel 09-20-2004 09:00 AM

Re: Replacing spaces
 
Jürgen Exner <jurgenex@hotmail.com> wrote in comp.lang.perl.misc:
> Aristotle wrote:
> > I'm trying to replace spaces at the beggining of a string, with
> > "&nbsp;" .
> > Not all spaces by a single &nbsp; , but rather each space by a single
> > "&nbsp;"
> >
> > eg
> > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
> > war"
> > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
> >
> > ie "&nbsp;" should replace only the beggining spaces (one by one), but
> > not other spaces.

>
> One way to do it:
>
> s/^(\s*)/'&nbsp;' x length $1/e;


Here's another:

/ */g;
substr( $_, 0, pos) =~ s/ /&nbsp;/g;

Anno



All times are GMT. The time now is 08:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.