FAQ 6.4 I put a regular expression into $/ but it didn"t work. What"s wrong?
FAQ 6.4 I put a regular expression into $/ but it didn"t work. What"s wrong?
am 27.12.2007 21:03:02 von PerlFAQ Server
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
------------------------------------------------------------ --------
6.4: I put a regular expression into $/ but it didn't work. What's wrong?
Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
don't get your hopes up. Until then, you can use these examples if you
really need to do this.
If you have File::Stream, this is easy.
use File::Stream;
my $stream = File::Stream->new(
$filehandle,
separator => qr/\s*,\s*/,
);
print "$_\n" while <$stream>;
If you don't have File::Stream, you have to do a little more work.
You can use the four argument form of sysread to continually add to a
buffer. After you add to the buffer, you check if you have a complete
line (using your regular expression).
local $_ = "";
while( sysread FH, $_, 8192, length ) {
while( s/^((?s).*?)your_pattern/ ) {
my $record = $1;
# do stuff here.
}
}
You can do the same thing with foreach and a match using the
c flag and the \G anchor, if you do not mind your entire file
being in memory at the end.
local $_ = "";
while( sysread FH, $_, 8192, length ) {
foreach my $record ( m/\G((?s).*?)your_pattern/gc ) {
# do stuff here.
}
substr( $_, 0, pos ) = "" if pos;
}
------------------------------------------------------------ --------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
Re: FAQ 6.4 I put a regular expression into $/ but it didn"t work. What"s wrong?
am 27.12.2007 22:35:11 von Ben Morrow
Quoth PerlFAQ Server :
>
> 6.4: I put a regular expression into $/ but it didn't work. What's wrong?
>
> Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
> don't get your hopes up. Until then, you can use these examples if you
s/8/10/; s/10/12/;
:)
Ben
Re: FAQ 6.4 I put a regular expression into $/ but it didn"t work. What"s wrong?
am 28.12.2007 01:07:32 von brian d foy
In article , Ben Morrow
wrote:
> Quoth PerlFAQ Server :
> >
> > 6.4: I put a regular expression into $/ but it didn't work. What's wrong?
> >
> > Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
> > don't get your hopes up. Until then, you can use these examples if you
>
> s/8/10/; s/10/12/;
I think we've gone through the faq to change all the 5.8s to 5.10s and
so on. I haven't refreshed the copy the PerlFAQ server is using,
though.
I'll look at perlfaq6 to check this one again.
Thanks, :)
s/A/B/ and s/B/C/ but don"t want A -> C (was: FAQ 6.4 I put a regular expression into $/ but it d
am 28.12.2007 19:52:26 von Ted Zlatanov
On Thu, 27 Dec 2007 21:35:11 +0000 Ben Morrow wrote:
BM> Quoth PerlFAQ Server :
>>
>> 6.4: I put a regular expression into $/ but it didn't work. What's wrong?
>>
>> Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
>> don't get your hopes up. Until then, you can use these examples if you
BM> s/8/10/; s/10/12/;
You want that backwards :)
Actually, I've often run into the need for parallel edits like this,
where you want to s/A/B/ and s/B/C but you don't want A to become C.
With complex operations or operations you don't know in advance, proper
ordering becomes impossible. I usually handle it with function calls:
s/[ABC]/replacement(\1)/e or something like that. Is there a better
approach anyone can recommend?
Thanks
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C (was: FAQ 6.4 I put a regular expression into $/ but
am 28.12.2007 20:47:50 von Michele Dondi
On Fri, 28 Dec 2007 12:52:26 -0600, Ted Zlatanov
wrote:
>BM> s/8/10/; s/10/12/;
>
>You want that backwards :)
>
>Actually, I've often run into the need for parallel edits like this,
>where you want to s/A/B/ and s/B/C but you don't want A to become C.
>With complex operations or operations you don't know in advance, proper
>ordering becomes impossible. I usually handle it with function calls:
>s/[ABC]/replacement(\1)/e or something like that. Is there a better
I believe you want capturing parens in the regex and $1 rather than \1
in the substitution.
>approach anyone can recommend?
Well, similar to yours, except not necessarily with /e (often a hash
is enough):
s/([ABC])/$replacement{$1}/g;
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: s/A/B/ and s/B/C/ but don"t want A -> C (was: FAQ 6.4 I put a
am 28.12.2007 20:51:53 von it_says_BALLS_on_your forehead
On Dec 28, 1:52=A0pm, Ted Zlatanov wrote:
> Actually, I've often run into the need for parallel edits like this,
> where you want to s/A/B/ and s/B/C but you don't want A to become C.
> With complex operations or operations you don't know in advance, proper
> ordering becomes impossible. =A0I usually handle it with function calls:
> s/[ABC]/replacement(\1)/e or something like that. =A0Is there a better
> approach anyone can recommend?
>
What does the above do? Shouldn't that be:
s/[ABC]/replacement( $1 )/e;
?
And could you please expound on "With complex operations or operations
you don't know in advance..."?
I don't see how you could avoid doing an inadvertent A->C if you did
multiple s/// out of order, unless you provided enough context for the
regex to work correctly.
Re: s/A/B/ and s/B/C/ but don"t want A -> C (was: FAQ 6.4 I put a
am 28.12.2007 20:52:49 von it_says_BALLS_on_your forehead
On Dec 28, 2:51=A0pm, nolo contendere wrote:
> On Dec 28, 1:52=A0pm, Ted Zlatanov wrote:
>
> > Actually, I've often run into the need for parallel edits like this,
> > where you want to s/A/B/ and s/B/C but you don't want A to become C.
> > With complex operations or operations you don't know in advance, proper
> > ordering becomes impossible. =A0I usually handle it with function calls:=
> > s/[ABC]/replacement(\1)/e or something like that. =A0Is there a better
> > approach anyone can recommend?
>
> What does the above do? Shouldn't that be:
> s/[ABC]/replacement( $1 )/e;
>
> ?
>
> And could you please expound on "With complex operations or operations
> you don't know in advance..."?
> I don't see how you could avoid doing an inadvertent A->C if you did
> multiple s/// out of order, unless you provided enough context for the
> regex to work correctly.
Oh yeah, just saw Michele's post. I forgot the capturing parens,
should be:
s/([ABC])/replacement( $1 )/e;
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 28.12.2007 21:11:45 von Ted Zlatanov
On Fri, 28 Dec 2007 11:52:49 -0800 (PST) nolo contendere wrote:
nc> On Dec 28, 2:51 pm, nolo contendere wrote:
>> On Dec 28, 1:52 pm, Ted Zlatanov wrote:
>>
>> > Actually, I've often run into the need for parallel edits like this,
>> > where you want to s/A/B/ and s/B/C but you don't want A to become C.
>> > With complex operations or operations you don't know in advance, proper
>> > ordering becomes impossible. I usually handle it with function calls:
>> > s/[ABC]/replacement(\1)/e or something like that. Is there a better
>> > approach anyone can recommend?
>>
>> What does the above do? Shouldn't that be:
>> s/[ABC]/replacement( $1 )/e;
>>
>> ?
>>
>> And could you please expound on "With complex operations or operations
>> you don't know in advance..."?
>> I don't see how you could avoid doing an inadvertent A->C if you did
>> multiple s/// out of order, unless you provided enough context for the
>> regex to work correctly.
nc> Oh yeah, just saw Michele's post. I forgot the capturing parens,
nc> should be:
nc> s/([ABC])/replacement( $1 )/e;
I forgot the parenthesis, but it doesn't change my question. \1 works
like $1 in this case, I used it out of habit.
Look at my example. When you are given a list of "X -> Y"
transformations, you don't know in advance how to order them so they
won't overlap (as in the A to B to C example). You need to look through
the list. The right ordering may not even be possible (if there's a
loop in the graph). That's what I meant by "don't know in advance." By
"complex" I mean that the overlap may not be obvious if the
replacement's output is not a simple function of the input.
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 28.12.2007 21:14:00 von Ted Zlatanov
On Fri, 28 Dec 2007 20:47:50 +0100 Michele Dondi wrote:
>> approach anyone can recommend?
MD> Well, similar to yours, except not necessarily with /e (often a hash
MD> is enough):
MD> s/([ABC])/$replacement{$1}/g;
That works too, much faster but it can only do static strings. I didn't
think of it. Thanks.
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 28.12.2007 21:22:41 von it_says_BALLS_on_your forehead
On Dec 28, 3:11=A0pm, Ted Zlatanov wrote:
> On Fri, 28 Dec 2007 11:52:49 -0800 (PST) nolo contendere
om> wrote:
>
> nc> On Dec 28, 2:51=A0pm, nolo contendere wrote:
>
>
>
> >> On Dec 28, 1:52=A0pm, Ted Zlatanov wrote:
>
> >> > Actually, I've often run into the need for parallel edits like this,
> >> > where you want to s/A/B/ and s/B/C but you don't want A to become C.
> >> > With complex operations or operations you don't know in advance, prop=
er
> >> > ordering becomes impossible. =A0I usually handle it with function cal=
ls:
> >> > s/[ABC]/replacement(\1)/e or something like that. =A0Is there a bette=
r
> >> > approach anyone can recommend?
>
> >> What does the above do? Shouldn't that be:
> >> s/[ABC]/replacement( $1 )/e;
>
> >> ?
>
> >> And could you please expound on "With complex operations or operations
> >> you don't know in advance..."?
> >> I don't see how you could avoid doing an inadvertent A->C if you did
> >> multiple s/// out of order, unless you provided enough context for the
> >> regex to work correctly.
>
> nc> Oh yeah, just saw Michele's post. I forgot the capturing parens,
> nc> should be:
> nc> s/([ABC])/replacement( $1 )/e;
>
> I forgot the parenthesis, but it doesn't change my question. =A0\1 works
> like $1 in this case, I used it out of habit.
>
> Look at my example. =A0When you are given a list of "X -> Y"
> transformations, you don't know in advance how to order them so they
> won't overlap (as in the A to B to C example). =A0You need to look through=
> the list. =A0The right ordering may not even be possible (if there's a
> loop in the graph). =A0That's what I meant by "don't know in advance." =A0=
By
> "complex" I mean that the overlap may not be obvious if the
> replacement's output is not a simple function of the input.
>
Ahh, gotcha. So you're talking about more of a line-by-line processing
approach than a multiple s/// on the same string.
Re: s/A/B/ and s/B/C/ but don"t want A -> C (was: FAQ 6.4 I put a regular expression into $/ but
am 29.12.2007 00:49:03 von Abigail
_
Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXII September MCMXCIII in
:
&& On Thu, 27 Dec 2007 21:35:11 +0000 Ben Morrow wrote:
&&
&& BM> Quoth PerlFAQ Server :
&& >>
&& >> 6.4: I put a regular expression into $/ but it didn't work. What's wrong?
&& >>
&& >> Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
&& >> don't get your hopes up. Until then, you can use these examples if you
&&
&& BM> s/8/10/; s/10/12/;
&&
&& You want that backwards :)
&&
&& Actually, I've often run into the need for parallel edits like this,
&& where you want to s/A/B/ and s/B/C but you don't want A to become C.
&& With complex operations or operations you don't know in advance, proper
&& ordering becomes impossible. I usually handle it with function calls:
&& s/[ABC]/replacement(\1)/e or something like that. Is there a better
&& approach anyone can recommend?
If A, B and C are just single characters, tr/AB/BC/ will do.
Otherwise, I'd do:
my %replacement = (A => B, B => C);
s/(A|B)/$replacement{$1}/g;
Abigail
--
perl -wle 'eval {die [[qq [Just another Perl Hacker]]]};; print
${${${@}}[$#{@{${@}}}]}[$#{${@{${@}}}[$#{@{${@}}}]}]'
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 29.12.2007 01:23:55 von Abigail
_
Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXII September MCMXCIII in
:
)) On Fri, 28 Dec 2007 20:47:50 +0100 Michele Dondi wrote:
))
)) >> approach anyone can recommend?
))
)) MD> Well, similar to yours, except not necessarily with /e (often a hash
)) MD> is enough):
))
)) MD> s/([ABC])/$replacement{$1}/g;
))
)) That works too, much faster but it can only do static strings. I didn't
)) think of it. Thanks.
With 5.10, you can do it with A and B patterns:
use 5.010;
our $REGMARK;
my %replacement = qw [X B Y C];
$_ = "AABDAAABBE";
s/(*:X)A(?{})|(*:Y)B/$replacement{$REGMARK}/g;
say;
__END__
BCDBCE
Note that the (?{}) is there to prevent a bug from triggering.
Abigail
--
print v74.117.115.116.32, v97.110.111.116.104.101.114.32,
v80.101.114.108.32, v72.97.99.107.101.114.10;
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 29.12.2007 14:45:17 von rvtol+news
Abigail schreef:
> my %replacement = (A => B, B => C);
> s/(A|B)/$replacement{$1}/g;
If string-A can ever be a subpattern of string-B, then you need to add a
"longest first" approach.
$ echo "AABABA" | perl -wpe '
BEGIN{ %repl = (A => B, AB => C); }
s/(A|AB)/$repl{$1}/g;
'
BBBBBB
$ echo "AABABA" | perl -wpe '
BEGIN{ %repl = (A => B, AB => C); }
s/(AB|A)/$repl{$1}/g; # longest first
'
BCCB
(or use a different regex-engine :)
--
Affijn, Ruud
"Gewoon is een tijger."
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 31.12.2007 15:09:18 von Ted Zlatanov
On 29 Dec 2007 00:23:55 GMT Abigail wrote:
A> With 5.10, you can do it with A and B patterns:
A> use 5.010;
A> our $REGMARK;
A> my %replacement = qw [X B Y C];
A> $_ = "AABDAAABBE";
A> s/(*:X)A(?{})|(*:Y)B/$replacement{$REGMARK}/g;
A> say;
A> __END__
A> BCDBCE
A> Note that the (?{}) is there to prevent a bug from triggering.
On Sat, 29 Dec 2007 14:45:17 +0100 "Dr.Ruud" wrote:
R> If string-A can ever be a subpattern of string-B, then you need to add a
R> "longest first" approach.
....
R> (or use a different regex-engine :)
Abigail's 5.10 solution doesn't scale well, and Dr. Ruud's longest-first
solution (which others suggested but without the longest-first fix) is
closest to a generic solution. It won't help if the list contains
regular expressions, but for fixed strings it's the best I can see. I'd
just build the match alternation with something like this:
sprintf "(%s)", join('|', sort { length $a <=> length $b } keys %patterns);
Thanks for all the suggestions.
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 01.01.2008 00:18:23 von Abigail
_
Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXV September MCMXCIII in
:
^^ On 29 Dec 2007 00:23:55 GMT Abigail wrote:
^^
^^ A> With 5.10, you can do it with A and B patterns:
^^
^^ A> use 5.010;
^^ A> our $REGMARK;
^^ A> my %replacement = qw [X B Y C];
^^ A> $_ = "AABDAAABBE";
^^ A> s/(*:X)A(?{})|(*:Y)B/$replacement{$REGMARK}/g;
^^ A> say;
^^ A> __END__
^^ A> BCDBCE
^^
^^ A> Note that the (?{}) is there to prevent a bug from triggering.
^^
^^ On Sat, 29 Dec 2007 14:45:17 +0100 "Dr.Ruud" wrote:
^^
^^ R> If string-A can ever be a subpattern of string-B, then you need to add a
^^ R> "longest first" approach.
^^ ...
^^ R> (or use a different regex-engine :)
^^
^^ Abigail's 5.10 solution doesn't scale well, and Dr. Ruud's longest-first
In which sense doesn't it scale well?
Abigail
--
:$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 02.01.2008 15:27:08 von Ted Zlatanov
On 31 Dec 2007 23:18:23 GMT Abigail wrote:
A> Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXV September MCMXCIII in
A> :
A> ^^ On 29 Dec 2007 00:23:55 GMT Abigail wrote:
A> ^^
A> ^^ A> With 5.10, you can do it with A and B patterns:
A> ^^
A> ^^ A> use 5.010;
A> ^^ A> our $REGMARK;
A> ^^ A> my %replacement = qw [X B Y C];
A> ^^ A> $_ = "AABDAAABBE";
A> ^^ A> s/(*:X)A(?{})|(*:Y)B/$replacement{$REGMARK}/g;
A> ^^ A> say;
A> ^^ A> __END__
A> ^^ A> BCDBCE
A> ^^
A> ^^ A> Note that the (?{}) is there to prevent a bug from triggering.
A> ^^
A> ^^ On Sat, 29 Dec 2007 14:45:17 +0100 "Dr.Ruud" wrote:
A> ^^
A> ^^ R> If string-A can ever be a subpattern of string-B, then you need to add a
A> ^^ R> "longest first" approach.
A> ^^ ...
A> ^^ R> (or use a different regex-engine :)
A> ^^
A> ^^ Abigail's 5.10 solution doesn't scale well, and Dr. Ruud's longest-first
A> In which sense doesn't it scale well?
I think it's harder to build the pattern you showed for large numbers of
strings. Am I wrong? Sorry if I misunderstood your example.
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 02.01.2008 16:33:25 von Abigail
_
Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXVII September MCMXCIII in
:
{} On 31 Dec 2007 23:18:23 GMT Abigail wrote:
{}
{} A> Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXV September MCMXCIII in
{} A> :
{} A> ^^ On 29 Dec 2007 00:23:55 GMT Abigail wrote:
{} A> ^^
{} A> ^^ A> With 5.10, you can do it with A and B patterns:
{} A> ^^
{} A> ^^ A> use 5.010;
{} A> ^^ A> our $REGMARK;
{} A> ^^ A> my %replacement = qw [X B Y C];
{} A> ^^ A> $_ = "AABDAAABBE";
{} A> ^^ A> s/(*:X)A(?{})|(*:Y)B/$replacement{$REGMARK}/g;
{} A> ^^ A> say;
{} A> ^^ A> __END__
{} A> ^^ A> BCDBCE
{} A> ^^
{} A> ^^ A> Note that the (?{}) is there to prevent a bug from triggering.
{} A> ^^
{} A> ^^ On Sat, 29 Dec 2007 14:45:17 +0100 "Dr.Ruud" wrote:
{} A> ^^
{} A> ^^ R> If string-A can ever be a subpattern of string-B, then you need to add a
{} A> ^^ R> "longest first" approach.
{} A> ^^ ...
{} A> ^^ R> (or use a different regex-engine :)
{} A> ^^
{} A> ^^ Abigail's 5.10 solution doesn't scale well, and Dr. Ruud's longest-first
{}
{} A> In which sense doesn't it scale well?
{}
{} I think it's harder to build the pattern you showed for large numbers of
{} strings. Am I wrong? Sorry if I misunderstood your example.
"Harder" in the sense of "more typing", yes.
But is there a solution that doesn't require more typing if there
are more strings?
Abigail
--
perl -we '$| = 1; $_ = "Just another Perl Hacker\n"; print
substr $_ => 0, 1 => "" while $_ && sleep 1 => 1'
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 02.01.2008 18:05:10 von Ted Zlatanov
On 02 Jan 2008 15:33:25 GMT Abigail wrote:
A> But is there a solution that doesn't require more typing if there
A> are more strings?
I mean that you can say, with the "simple" approach:
my %reps = ( typing => 'only', goes => 'here', t=>'x');
my @words = sort { length $b <=> length $a } keys %reps;
my $pattern = '(' . join('|', @words) . ')'; # I know I could do it in one step
while(<>)
{
s/$pattern/$reps{$1}/g;
print;
}
The only work needed is to populate %reps, but that is more typing with
any approach :)
With yours it's harder to build the match pattern, I think, that's why I
asked you if I'm misunderstanding it.
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 02.01.2008 18:06:56 von Abigail
_
Ted Zlatanov (tzz@lifelogs.com) wrote on VCCXXXVII September MCMXCIII in
:
[] On 02 Jan 2008 15:33:25 GMT Abigail wrote:
[]
[] A> But is there a solution that doesn't require more typing if there
[] A> are more strings?
[]
[] I mean that you can say, with the "simple" approach:
[]
[] my %reps = ( typing => 'only', goes => 'here', t=>'x');
[] my @words = sort { length $b <=> length $a } keys %reps;
[] my $pattern = '(' . join('|', @words) . ')'; # I know I could do it in one step
[]
[] while(<>)
[] {
[] s/$pattern/$reps{$1}/g;
[] print;
[] }
[]
[] The only work needed is to populate %reps, but that is more typing with
[] any approach :)
Yeah, but now you're back to only being able to replace 'fixed strings',
while I posted by approach to be able to deal with patterns.
[] With yours it's harder to build the match pattern, I think, that's why I
[] asked you if I'm misunderstanding it.
Sure, it's harder than the "simple approach". But it's more powerful.
Abigail
--
perl -we 'eval {die ["Just another Perl Hacker\n"]}; print ${${@}}[$#{@{${@}}}]'
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 02.01.2008 21:09:10 von Ted Zlatanov
On 02 Jan 2008 17:06:56 GMT Abigail wrote:
A> Yeah, but now you're back to only being able to replace 'fixed strings',
A> while I posted by approach to be able to deal with patterns.
That's what I misunderstood. Thanks for clarifying.
Ted
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 14.01.2008 14:44:18 von Michele Dondi
On 02 Jan 2008 17:06:56 GMT, Abigail wrote:
>[] The only work needed is to populate %reps, but that is more typing with
>[] any approach :)
>
>
>Yeah, but now you're back to only being able to replace 'fixed strings',
>while I posted by approach to be able to deal with patterns.
>
>[] With yours it's harder to build the match pattern, I think, that's why I
>[] asked you if I'm misunderstanding it.
>
>
>Sure, it's harder than the "simple approach". But it's more powerful.
I'm trying to follow this discussion and I'm not acquainted yet with
the new constructs. Anyway the question is: how easy is it to
construct a sub that given a list of patterns (and substitutions)
would assemble a pattern satisfying the "condition of the Subject"?
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 14.01.2008 14:59:04 von Abigail
_
Michele Dondi (bik.mido@tiscalinet.it) wrote on VCCXLIX September
MCMXCIII in :
// On 02 Jan 2008 17:06:56 GMT, Abigail wrote:
//
// >[] The only work needed is to populate %reps, but that is more typing with
// >[] any approach :)
// >
// >
// >Yeah, but now you're back to only being able to replace 'fixed strings',
// >while I posted by approach to be able to deal with patterns.
// >
// >[] With yours it's harder to build the match pattern, I think, that's why I
// >[] asked you if I'm misunderstanding it.
// >
// >
// >Sure, it's harder than the "simple approach". But it's more powerful.
//
// I'm trying to follow this discussion and I'm not acquainted yet with
// the new constructs. Anyway the question is: how easy is it to
// construct a sub that given a list of patterns (and substitutions)
// would assemble a pattern satisfying the "condition of the Subject"?
How easy? "Quite".
Abigail
--
perl -le 's[$,][join$,,(split$,,($!=85))[(q[0006143730380126152532042 307].
q[41342211132019313505])=~m[..]g]]e and y[yIbp][HJkP] and print'
Re: s/A/B/ and s/B/C/ but don"t want A -> C
am 14.01.2008 15:32:12 von Ben Morrow
Quoth Michele Dondi :
> On 02 Jan 2008 17:06:56 GMT, Abigail wrote:
>
> >[] The only work needed is to populate %reps, but that is more typing with
> >[] any approach :)
> >
> >Yeah, but now you're back to only being able to replace 'fixed strings',
> >while I posted by approach to be able to deal with patterns.
> >
> >[] With yours it's harder to build the match pattern, I think, that's why I
> >[] asked you if I'm misunderstanding it.
> >
> >Sure, it's harder than the "simple approach". But it's more powerful.
>
> I'm trying to follow this discussion and I'm not acquainted yet with
> the new constructs. Anyway the question is: how easy is it to
> construct a sub that given a list of patterns (and substitutions)
> would assemble a pattern satisfying the "condition of the Subject"?
If your strings don't contain ')', then it appears to be as easy as
my %repl = (
'ab?c' => 'ONE',
'xyz+' => 'TWO',
);
my $rx =
map qr/$_/,
'(?{})' . # bug in 5.10.0
join '|',
map "(*:$repl{$_})$_",
keys %repl;
our $REGMARK;
s/$rx/$REGMARK/g;
I don't know whether the fact that the 'NAME' part of the (*MARK:NAME)
constructs can contain anything except ')' is a bug or a feature, but it
works with 5.10.0. For arbitrary strings it would be safer to build a
second hash, giving each replacement a (safe) name and using that in the
(*:NAME) construction.
The way this works is it builds a pattern like (adding whitespace and
ignoring the initial (?{}) that shouldn't be there)
(*:ONE) ab?c | (*:TWO) xyz+
which will set $REGMARK to the (*:) in the branch that actually matched.
Ben