Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > in ed(1) one can do s///n

Reply
Thread Tools

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

 
 
Dan Jacobson
Guest
Posts: n/a
 
      01-09-2006
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?
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      01-09-2006
Dan Jacobson wrote:
> 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...

--
http://DavidFilmer.com

 
Reply With Quote
 
 
 
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      01-09-2006

Dan Jacobson wrote:
> 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,
--
Charles DeRykus

 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      01-09-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Dan Jacobson wrote:
> > 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.
> ^^^^^^^^^^^^


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,
--
Charles DeRykus

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-09-2006
(E-Mail Removed):
> Dan Jacobson:


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


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?

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-09-2006
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

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-10-2006
(E-Mail Removed) <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> (E-Mail Removed) wrote:
> > Dan Jacobson wrote:
> > > 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.
> > ^^^^^^^^^^^^

>
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-10-2006
Anno Siegel <(E-Mail Removed)-berlin.de> wrote in comp.lang.perl.misc:
> (E-Mail Removed) <(E-Mail Removed)> wrote in
> comp.lang.perl.misc:
> >
> > (E-Mail Removed) wrote:
> > > Dan Jacobson wrote:
> > > > 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.
> > > ^^^^^^^^^^^^

> >
> > 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/;


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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-10-2006
Anno Siegel <(E-Mail Removed)-berlin.de> wrote in comp.lang.perl.misc:
> Anno Siegel <(E-Mail Removed)-berlin.de> wrote in comp.lang.perl.misc:
> > (E-Mail Removed) <(E-Mail Removed)> wrote in
> > comp.lang.perl.misc:
> > >
> > > (E-Mail Removed) wrote:
> > > > Dan Jacobson wrote:
> > > > > 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.
> > > > ^^^^^^^^^^^^
> > >
> > > 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/;

>
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Samsung SCD6040 - Anyone have one, used one, or have reviews on one? g wills Digital Photography 0 09-08-2004 08:06 PM
CSS aligning two things on one line with one left and one right news.frontiernet.net HTML 6 04-16-2004 02:44 AM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments