Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to do this with RE?

Reply
Thread Tools

how to do this with RE?

 
 
Arthur
Guest
Posts: n/a
 
      12-01-2006
Hi,

$foo is a string with leading spaces of variable number, like

" Willy"

What I want is to replace the leading spaces with   of the same
number as in $foo, so that

" Willy"

becomes

"   Willy"

(Don't know if I make myself understood?) How can I do this?

Thanks,
Arthur
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-01-2006
Arthur <(E-Mail Removed)> wrote:


> $foo is a string with leading spaces of variable number, like
>
> " Willy"



That is a pretty long-winded way of saying:

$foo = " Willy";

Have you seen the Posting Guidelines that are posted here frequently?


> What I want is to replace the leading spaces with &nbsp; of the same
> number as in $foo, so that
>
> " Willy"
>
> becomes
>
> "&nbsp;&nbsp;&nbsp;Willy"



$foo =~ s/^(\s+)/ '&nbsp;' x length $1/e;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      12-01-2006
Arthur <(E-Mail Removed)> wrote:

> Hi,
>
> $foo is a string with leading spaces of variable number, like
>
> " Willy"
>
> What I want is to replace the leading spaces with &nbsp; of the same
> number as in $foo, so that
>
> " Willy"
>
> becomes
>
> "&nbsp;&nbsp;&nbsp;Willy"
>
> (Don't know if I make myself understood?) How can I do this?


I guess your problem has nothing to do with Perl but with a lack of
understanding of HTML / CSS. Normally if you want to move Willy to the
right you use a margin on the container element.

--
John Experienced Perl programmer: http://castleamber.com/

Perl help, tutorials, and examples: http://johnbokma.com/perl/
 
Reply With Quote
 
Arthur
Guest
Posts: n/a
 
      12-01-2006
Tad McClellan Wrote:
> Arthur <(E-Mail Removed)> wrote:
>
>> $foo is a string with leading spaces of variable number, like
>> " Willy"

> That is a pretty long-winded way of saying:
>
> $foo = " Willy";
>
> Have you seen the Posting Guidelines that are posted here frequently?
>
>> What I want is to replace the leading spaces with &nbsp; of the same
>> number as in $foo, so that
>> " Willy"
>> becomes
>>
>> "&nbsp;&nbsp;&nbsp;Willy"

>
> $foo =~ s/^(\s+)/ '&nbsp;' x length $1/e;
>


Thanks, and apologies for my wordiness

Arthur
 
Reply With Quote
 
Arthur
Guest
Posts: n/a
 
      12-01-2006
John Bokma д:
> Arthur <(E-Mail Removed)> wrote:
>
>> Hi,
>>
>> $foo is a string with leading spaces of variable number, like
>>
>> " Willy"
>>
>> What I want is to replace the leading spaces with &nbsp; of the same
>> number as in $foo, so that
>>
>> " Willy"
>>
>> becomes
>>
>> "&nbsp;&nbsp;&nbsp;Willy"
>>
>> (Don't know if I make myself understood?) How can I do this?

>
> I guess your problem has nothing to do with Perl but with a lack of
> understanding of HTML / CSS. Normally if you want to move Willy to the
> right you use a margin on the container element.
>

In fact I wasn't trying to move Willy to the right. Some of the postings
on my web forum are written as poems, with some lines like that, so I
just want to display the lines as intended. (Trailing spaces cause
problems if replaced with &nbsp; because very long lines don't wrap
properly.)
 
Reply With Quote
 
Arthur
Guest
Posts: n/a
 
      12-01-2006
Andrew DeFaria д:
> Arthur wrote:
>> In fact I wasn't trying to move Willy to the right. Some of the
>> postings on my web forum are written as poems, with some lines like
>> that, so I just want to display the lines as intended. (Trailing
>> spaces cause problems if replaced with &nbsp; because very long lines
>> don't wrap properly.)

> <pre>
>
> --
>
> Andrew DeFaria <http://defaria.com>
> All women are idiots... and I married their queen.


I considered <pre>, but PREed paragraphs don't seem to wrap?
 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      12-01-2006
Arthur wrote:

> $foo is a string with leading spaces of variable number, like
>
> " Willy"
>
> What I want is to replace the leading spaces with &nbsp; of the same
> number as in $foo, so that
>
> " Willy"
>
> becomes
>
> "&nbsp;&nbsp;&nbsp;Willy"
>
> (Don't know if I make myself understood?) How can I do this?
>


Another way:

$foo = " Willy";
$foo =~ s/\G\s/&nbsp;/g;

print $foo; # -> &nbsp;&nbsp;&nbsp;Willy

--
Charles DeRykus


 
Reply With Quote
 
Steve K.
Guest
Posts: n/a
 
      12-01-2006
Tad McClellan wrote:
> Arthur <(E-Mail Removed)> wrote:
>
>
> > $foo is a string with leading spaces of variable number, like
> >
> > " Willy"

>
>
> That is a pretty long-winded way of saying:
>
> $foo = " Willy";
>
> Have you seen the Posting Guidelines that are posted here frequently?


I don't see them at all in my spool. I'm not sure when the last time it
was posted (haven't seen them in a while come to think of it), it's also
possible others my not be seeing it too. But then again, you and others
have been told this before by others.


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-01-2006
Charles DeRykus schreef:
> Arthur:


>> What I want is to replace the leading spaces with &nbsp;

>
> $foo = " Willy";
> $foo =~ s/\G\s/&nbsp;/g;


Nice example of \G usage.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Andrew DeFaria
Guest
Posts: n/a
 
      12-01-2006
Arthur wrote:
> Andrew DeFaria д:
>> Arthur wrote:
>>> In fact I wasn't trying to move Willy to the right. Some of the
>>> postings on my web forum are written as poems, with some lines like
>>> that, so I just want to display the lines as intended. (Trailing
>>> spaces cause problems if replaced with &nbsp; because very long
>>> lines don't wrap properly.)

> I considered <pre>, but PREed paragraphs don't seem to wrap?

First you say you want it to remain as intended and now you want to
alter it! Make up your mind!

You could try:

pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}

--

Andrew DeFaria <http://defaria.com>
Even a mosquito doesn't get a slap on the back until it starts to work.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments