in ed(1) one can do s///n

D

Dan Jacobson

How can one live without little old ed(1)'s s///n?
(.,.)s/re/replacement/n
The `n' suffix, where n is a postive number, causes only the nth
match to be replaced.
s/a/A/13p
aaaaaaaaaaaaAaaaaaaa
How can one do s///n in perl without jumping through hoops?
 
U

usenet

Dan said:
s/a/A/13p
aaaaaaaaaaaaAaaaaaaa
How can one do s///n in perl without jumping through hoops?

I'm not sure what you consider a hoop; you can do this:

$string =~ s/(a{12})a/$1A/;

Not as clean and easy as ed(), I'll agree... but there's probably a
better way to do it in Perl also...
 
C

ced

Dan said:
How can one live without little old ed(1)'s s///n?
(.,.)s/re/replacement/n
The `n' suffix, where n is a postive number, causes only the nth
match to be replaced.
s/a/A/13p
aaaaaaaaaaaaAaaaaaaa
How can one do s///n in perl without jumping through hoops?

my $n = 3; # 3rd match subst.
my $i = 0;
s/ a (?{$i++}) / $i == $n ? 'A' : 'a' /gex;

There are clearer ways though maybe not as short.

hth,
 
C

ced

my $n = 3; # 3rd match subst.
my $i = 0;
s/ a (?{$i++}) / $i == $n ? 'A' : 'a' /gex;

There are clearer ways though maybe not as short.
^^^^^^^^^^^^

See David F.'s shorter solution. This is a bit more general if the
the intent was to handle cases where the a's weren't contiguous.

hth,
 
D

Dr.Ruud

(e-mail address removed):
Dan Jacobson:

I'm not sure what you consider a hoop; you can do this:

$string =~ s/(a{12})a/$1A/;

Not as clean and easy as ed(), I'll agree...
Variants:

s/(?<=a{12})a/A/

s/(?<=(a){12})\1/A/


but there's probably a
better way to do it in Perl also...

I sure hope so. Because I used sed a lot, I am used to that counter.
Maybe Perl6 has such a quantifier?
 
D

Dr.Ruud

Dr.Ruud schreef:
Maybe Perl6 has such a quantifier?

It doesn't apply, but I found this in A6:

s:3///; # do 3 times

Looks like that could use a range:

s:3///; # do the 3rd time
s:0..2///; # do 3 times
s:^3///; # do 3 times
 
A

Anno Siegel

See David F.'s shorter solution. This is a bit more general if the
the intent was to handle cases where the a's weren't contiguous.

This handles non-contiguous matches too:

my $str = 'aXXXaaXXXaXaaX';
my $n = 3;

$str =~ /a/g for 1 .. $n - 1;
$str =~ s/\Ga/b/;

Anno
 
A

Anno Siegel

Anno Siegel said:
This handles non-contiguous matches too:

my $str = 'aXXXaaXXXaXaaX';
my $n = 3;

$str =~ /a/g for 1 .. $n - 1;
$str =~ s/\Ga/b/;

Ah, but this needs some work. With the given string it works for $n = 3
but not for $n = 4. Better, but less pretty:

$str =~ /a/g for 1 .. $n - 1;
$str =~ s/\G(.*?)a/$1b/;

Anno
 
A

Anno Siegel

Anno Siegel said:
Ah, but this needs some work. With the given string it works for $n = 3
but not for $n = 4. Better, but less pretty:

$str =~ /a/g for 1 .. $n - 1;
$str =~ s/\G(.*?)a/$1b/;

Okay, here goes:

$str =~ /a/g for 1 .. $n;
$str =~ s/a\G/b/;

Anno
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top