Click here to get back home

s replace p modifier

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
s replace p modifier Lore Leunoeg 04-14-2008
Posted by Lore Leunoeg on April 14, 2008, 7:13 pm
Please log in for more thread options
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



Posted by A. Sinan Unur on April 14, 2008, 7:13 pm
Please log in for more thread options

> 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

--
(remove .invalid and reverse each component for email address)

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

Posted by Tad J McClellan on April 14, 2008, 8:44 pm
Please log in for more thread options
> 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.noitatibaher0cmdat/"

Posted by szr on April 14, 2008, 11:24 pm
Please log in for more thread options
Tad J McClellan 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



Posted by Abigail on April 15, 2008, 4:53 am
Please log in for more thread options
_
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 $.

-- 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 $. The following ought to do (not tested):

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


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

Similar ThreadsPosted
Prematch ($`) and the m//g modifier March 9, 2006, 8:09 pm
qr// doesn't handle m modifier? September 1, 2006, 1:57 pm
Why doesn't it evaluate in RE using "e" modifier ?? January 2, 2008, 5:50 am
/e modifier to s///; short "1 liner" ? December 7, 2007, 1:26 pm
s/$match/$replace/ fails when $replace has backreferences September 5, 2005, 7:05 pm
Replace with nothing May 12, 2005, 7:54 am
HOW TO replace ' but not ?' October 27, 2004, 4:30 pm
Help: Replace Help May 1, 2008, 8:29 am
search & replace September 7, 2004, 12:27 pm
search and replace help April 22, 2005, 2:11 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap