s replace p modifier

s replace p modifier

am 15.04.2008 01:13:05 von 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

Re: s replace p modifier

am 15.04.2008 01:13:09 von 1usa

"Lore Leunoeg" wrote in
news:fu0o1b$vt0$1@news01.versatel.de:

> 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 ( ) {
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 <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

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

Re: s replace p modifier

am 15.04.2008 02:44:12 von Tad J McClellan

Lore Leunoeg wrote:
> 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.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: s replace p modifier

am 15.04.2008 05:24:51 von szr

Tad J McClellan wrote:
> Lore Leunoeg wrote:
>> 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.)

--
szr

Re: s replace p modifier

am 15.04.2008 10:53:14 von Abigail

_
Lore Leunoeg (loreleunoeg@gmx.net) wrote on VCCCXL September MCMXCIII in
:
-- 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).

It really would help if you used the documentation that comes with your Perl.
None of the 5.8 versions will document the /p modifier. It wasn't documented
until perl 5.10, which does know the /p modifier.

But it doesn't say $<^MATCH>. The variable is called ${^MATCH}.

-- 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?

No need for /p and ${^MATCH}. The following ought to do (not tested):

s/([0-9]+)/\$$1\$/g;


Abigail
--
# Perl 5.6.0 broke this.
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC ;1while+s/(\d+)((.)
(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers of Hanoi