Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How can I search and replace a string while preserving (not removing) trailing spaces?

Reply
Thread Tools

How can I search and replace a string while preserving (not removing) trailing spaces?

 
 
rsarpi
Guest
Posts: n/a
 
      05-13-2007
Sorry the dumb question. I'm a newbie.

But how can I search and replace a string while preserving (not
removing) trailing spaces?

let me explain: the command 's/$old_string/$new_string/g' inserts the
new string and *moves* spaces to the right or left (depending on the
length of the 'new_string') making the whole sentence bigger or
smaller.

for example:

my $sentence = "*Peter Parker is Spider Man *";
print length($sentence); #prints 43 characters

I'd like to know a trick in which, if I get rid of "Peter" I would
_still_ get 43 characters in length for the whole sentence between
both quotation marks. Here the whole sentence between quotation marks
expands until it reaches 43 chars in length.

my $sentence = "*Parker is Spider Man *";
print length($sentence );#still 43


And if I add to the sentence Benjamin (between Peter and Parker) I
would like to get exactly 43 characters between both quotation marks.
Here the whole sentence between quotation marks shrinks until it gets
up 43 chars in length.

my $sentence = "*Peter Benjamin Parker is Spider Man *";
print length($sentence ); #still 43

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      05-13-2007
rsarpi wrote:
> Sorry the dumb question. I'm a newbie.
>
> But how can I search and replace a string while preserving (not
> removing) trailing spaces?
>
> let me explain: the command 's/$old_string/$new_string/g' inserts the
> new string and *moves* spaces to the right or left (depending on the
> length of the 'new_string') making the whole sentence bigger or
> smaller.
>
> for example:
>
> my $sentence = "*Peter Parker is Spider Man *";
> print length($sentence); #prints 43 characters
>
> I'd like to know a trick in which, if I get rid of "Peter" I would
> _still_ get 43 characters in length for the whole sentence between
> both quotation marks. Here the whole sentence between quotation marks
> expands until it reaches 43 chars in length.
>
> my $sentence = "*Parker is Spider Man *";
> print length($sentence );#still 43
>
>
> And if I add to the sentence Benjamin (between Peter and Parker) I
> would like to get exactly 43 characters between both quotation marks.
> Here the whole sentence between quotation marks shrinks until it gets
> up 43 chars in length.
>
> my $sentence = "*Peter Benjamin Parker is Spider Man *";
> print length($sentence ); #still 43


$ perl -e'
my $sentence = "Peter Parker is Spider Man";
printf "*%-43.43s*\n", $sentence;
$sentence = "Parker is Spider Man";
printf "*%-43.43s*\n", $sentence;
$sentence = "Peter Benjamin Parker is Spider Man";
printf "*%-43.43s*\n", $sentence;
'
*Peter Parker is Spider Man *
*Parker is Spider Man *
*Peter Benjamin Parker is Spider Man *


perldoc -f printf
perldoc -f sprintf
perldoc -f pack
perldoc -f unpack



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
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
Sign preserving Vs value preserving sophia.agnes@gmail.com C Programming 4 12-07-2007 03:14 PM
How to copy a file on Windows while preserving permissions Andrew Koenig Python 1 04-12-2007 09:24 PM
integral promotion, arithmetic conversion, value preserving, unsigned preserving??? TTroy C Programming 16 01-31-2005 10:20 PM
How to backup a directory while preserving the modified date? JD Computer Support 1 09-16-2004 08:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57