how to count matches?

Discussion in 'Perl Misc' started by Geoff Cox, Apr 28, 2005.

  1. Geoff Cox

    Geoff Cox Guest

    Hello

    I want to use regex to find/replace say <jim> with $num-fred<jim> so
    that $num increase by 1 for each occurrence.

    It might that there are 2 of <jim> on a line so my code below is not
    working - how should I change it? Clues please!

    $num = 1;

    if ($line =~ /<jim>/) {
    my $orig = "<jim>";
    my $new = "$num-jim";
    $line =~ s/$orig/$new/g;
    $num++;
    }

    Thanks

    Geoff
    Geoff Cox, Apr 28, 2005
    #1
    1. Advertising

  2. Geoff Cox

    John Bokma Guest

    Geoff Cox wrote:

    > Hello
    >
    > I want to use regex to find/replace say <jim> with $num-fred<jim> so
    > that $num increase by 1 for each occurrence.
    >
    > It might that there are 2 of <jim> on a line so my code below is not
    > working - how should I change it? Clues please!
    >
    > $num = 1;
    >
    > if ($line =~ /<jim>/) {
    > my $orig = "<jim>";
    > my $new = "$num-jim";
    > $line =~ s/$orig/$new/g;
    > $num++;


    Not tested: s{$orig}{ $num++ . "-jim" }ge;

    note the e.

    Also note that the first part of your posting and your actual code are in
    disagreement.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Apr 28, 2005
    #2
    1. Advertising

  3. Geoff Cox <> writes:
    > I want to use regex to find/replace say <jim> with $num-fred<jim> so
    > that $num increase by 1 for each occurrence.
    >
    > It might that there are 2 of <jim> on a line so my code below is not
    > working - how should I change it? Clues please!
    >
    > $num = 1;
    >
    > if ($line =~ /<jim>/) {
    > my $orig = "<jim>";
    > my $new = "$num-jim";
    > $line =~ s/$orig/$new/g;
    > $num++;


    Use the 'e' modifier:

    if ($line =~ /<jim>/) {
    my $orig = "<jim>";
    $line =~ s/$orig/$num++ . "-jim"/ge;
    Arndt Jonasson, Apr 28, 2005
    #3
  4. Geoff Cox

    Geoff Cox Guest

    On 28 Apr 2005 10:48:10 GMT, John Bokma <> wrote:


    >> $num = 1;
    >>
    >> if ($line =~ /<jim>/) {
    >> my $orig = "<jim>";
    >> my $new = "$num-jim";
    >> $line =~ s/$orig/$new/g;
    >> $num++;


    ThanksJOhn - I have it working now.

    Cheers

    Geoff



    >
    >Not tested: s{$orig}{ $num++ . "-jim" }ge;
    >
    >note the e.
    >
    >Also note that the first part of your posting and your actual code are in
    >disagreement.
    Geoff Cox, Apr 28, 2005
    #4
  5. Geoff Cox

    Geoff Cox Guest

    On 28 Apr 2005 12:51:38 +0200, Arndt Jonasson <>
    wrote:

    >Use the 'e' modifier:
    >
    > if ($line =~ /<jim>/) {
    > my $orig = "<jim>";
    > $line =~ s/$orig/$num++ . "-jim"/ge;


    Thanks Arndt - all is well!

    Cheers

    Geoff
    Geoff Cox, Apr 28, 2005
    #5
    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. Boris Pelakh
    Replies:
    3
    Views:
    455
    Purl Gurl
    Apr 8, 2004
  2. nospam
    Replies:
    2
    Views:
    411
  3. Doug
    Replies:
    0
    Views:
    459
  4. efelnavarro09
    Replies:
    2
    Views:
    914
    efelnavarro09
    Jan 26, 2011
  5. Replies:
    6
    Views:
    153
Loading...

Share This Page