s replace p modifier

L

Lore Leunoeg

Hello

I'd like to encapsulate each number in a textfile with dollar-signs ($). I
thougt to replace each number by a $-sign followed by the pattern matched
number itself and another $-sign.

How can I get the exact pattern which was replaced by
s/PATTERN/PREPLACEMENT/modifier?
In the perl docs I read that the p preserve modifier would save the replaced
pattern in a variable $<^MATCH>. But the p modifier isn't known by my perl
installation (vers5.8).

Does anybody know another way to solve the encapsulation problem? Or does
anyone can give me a hint why the p modifier isn't known?

Thank you
Sincerely
Lore
 
A

A. Sinan Unur

I'd like to encapsulate each number in a textfile with
dollar-signs ($). I thougt to replace each number by a $-sign
followed by the pattern matched number itself and another $-sign.

OK, stop here. The rest of your post is extremely confusing to me
because I cannot see why you would need the p modifier.

#!/usr/bin/perl

use strict;
use warnings;

while ( <DATA> ) {
s/(\d+)/\$$1\$/g;
print "$_\n";
}

__DATA__
a 1 2 3
b 2 3 4
c 3 4 g
d 4 h i
e 5 9 a
Does anybody know another way to solve the encapsulation problem?

I don't know what *the* encapsulation problem is.
Or does anyone can give me a hint why the p modifier isn't known?

Because you don't have perl 5.10.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
S

szr

Tad said:
Lore Leunoeg said:
Hello

I'd like to encapsulate each number in a textfile with dollar-signs
($). I thougt to replace each number by a $-sign followed by the
pattern matched number itself and another $-sign.


That's a very good thought.

How can I get the exact pattern which was replaced by
s/PATTERN/PREPLACEMENT/modifier?


[ Please copy/paste code rather than (attempt to) retype it. ]


By enclosing the PATTERN in parenthesis and using the $1 variable
in the REPLACEMENT part.

In the perl docs I read that the p preserve modifier would save the
replaced pattern in a variable $<^MATCH>. But the p modifier isn't
known by my perl installation (vers5.8).


You should not read random Perl docs from the web.

You should read the Perl docs that *came with* your perl distribution.

They are already on your disk somewhere. Find out where.

Does anybody know another way to solve the encapsulation problem? Or
does anyone can give me a hint why the p modifier isn't known?


You were reading docs that did not apply to the software
that you currently have available.

That can never happen if you read the docs that came with the
software that you currently have available.

True enough, though it is possible to read the wrong documents if one's
system has multiple installs (for instance, one that came bundled with
your system and one and compiled by hand afterwards), and also some
people use systems they did not set up (i.e., in a work place
environment.)

If in doubt, invoke perldoc by absolute path.

For example, I my own system, I have 5.10.0 and 5.8.8 installed in
/usr/local/ perl5.8.8 and perl5.10.0 and have symlinked binaries from
each (perl, perldoc, etc) as /usr/local/bin perl5.8.8 and perldoc5.8.8
and similar for 5.10.0, respectively, that way I am always sure of which
one I am invoking (though I use 5.10.0 as my primary Perl now... perl
and perldoc are too symlinked to 5.10.0's binaries.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top