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

Discussion in 'Perl Misc' started by Dan Jacobson, Jan 9, 2006.

  1. Dan Jacobson

    Dan Jacobson Guest

    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?
     
    Dan Jacobson, Jan 9, 2006
    #1
    1. Advertising

  2. Dan Jacobson

    Guest

    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
     
    , Jan 9, 2006
    #2
    1. Advertising

  3. Dan Jacobson

    Guest

    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
     
    , Jan 9, 2006
    #3
  4. Dan Jacobson

    Guest

    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
     
    , Jan 9, 2006
    #4
  5. Dan Jacobson

    Dr.Ruud Guest

    :
    > 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."
     
    Dr.Ruud, Jan 9, 2006
    #5
  6. Dan Jacobson

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, Jan 9, 2006
    #6
  7. Dan Jacobson

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    >
    > 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.
     
    Anno Siegel, Jan 10, 2006
    #7
  8. Dan Jacobson

    Anno Siegel Guest

    Anno Siegel <-berlin.de> wrote in comp.lang.perl.misc:
    > <> wrote in
    > comp.lang.perl.misc:
    > >
    > > 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.
     
    Anno Siegel, Jan 10, 2006
    #8
  9. Dan Jacobson

    Anno Siegel Guest

    Anno Siegel <-berlin.de> wrote in comp.lang.perl.misc:
    > Anno Siegel <-berlin.de> wrote in comp.lang.perl.misc:
    > > <> wrote in
    > > comp.lang.perl.misc:
    > > >
    > > > 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.
     
    Anno Siegel, Jan 10, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    0
    Views:
    1,641
    Randall Parker
    Dec 4, 2005
  2. Sanjay Meher
    Replies:
    4
    Views:
    415
    Jon A. Cruz
    Feb 8, 2004
  3. news.frontiernet.net
    Replies:
    6
    Views:
    1,134
    news.frontiernet.net
    Apr 16, 2004
  4. loveNUNO
    Replies:
    2
    Views:
    933
    loveNUNO
    Nov 20, 2003
  5. Merciadri Luca
    Replies:
    4
    Views:
    828
Loading...

Share This Page