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
|