Counting occurances of string A in string B, and adding it to string B

Discussion in 'Perl Misc' started by Sandman, Aug 3, 2004.

  1. Sandman

    Sandman Guest

    Let's say I have a string that goes like this:

    "A horse is a horse is a horse, on a horseman"

    and I want to count how many "horse" there is in the string. Well, that's easy,
    by using:

    $_ = "A horse is a horse is a horse, on a horseman";
    my $nr;
    $nr++ for /horse/ig;
    print $nr;
    __END__
    Out: 4

    Now, I would like add a number to each word that contains the string horse, and
    this is where I am lost.

    Basically, what I want to output is this:

    "A horse (1) is a horse (2) is a horse (3), on a horseman (4)"


    Note that it should be "horseman (4)" not "horse (4)man".


    Anyone got a juicy regexp for this? :)

    --
    Sandman[.net]
     
    Sandman, Aug 3, 2004
    #1
    1. Advertising

  2. Sandman

    Anno Siegel Guest

    Sandman <> wrote in comp.lang.perl.misc:
    > Let's say I have a string that goes like this:
    >
    > "A horse is a horse is a horse, on a horseman"
    >
    > and I want to count how many "horse" there is in the string. Well, that's easy,
    > by using:
    >
    > $_ = "A horse is a horse is a horse, on a horseman";
    > my $nr;
    > $nr++ for /horse/ig;
    > print $nr;
    > __END__
    > Out: 4
    >
    > Now, I would like add a number to each word that contains the string horse, and
    > this is where I am lost.
    >
    > Basically, what I want to output is this:
    >
    > "A horse (1) is a horse (2) is a horse (3), on a horseman (4)"
    >
    >
    > Note that it should be "horseman (4)" not "horse (4)man".


    my $n = 0;
    s/(\w*horse\w*)/do { $n ++; "$1 ($n)"}/eg;

    Anno
     
    Anno Siegel, Aug 3, 2004
    #2
    1. Advertising

  3. Sandman

    Sandman Guest

    In article <cenrj5$ro6$-Berlin.DE>,
    -berlin.de (Anno Siegel) wrote:

    > Sandman <> wrote in comp.lang.perl.misc:
    > > Let's say I have a string that goes like this:
    > >
    > > "A horse is a horse is a horse, on a horseman"
    > >
    > > and I want to count how many "horse" there is in the string. Well, that's
    > > easy,
    > > by using:
    > >
    > > $_ = "A horse is a horse is a horse, on a horseman";
    > > my $nr;
    > > $nr++ for /horse/ig;
    > > print $nr;
    > > __END__
    > > Out: 4
    > >
    > > Now, I would like add a number to each word that contains the string horse,
    > > and
    > > this is where I am lost.
    > >
    > > Basically, what I want to output is this:
    > >
    > > "A horse (1) is a horse (2) is a horse (3), on a horseman (4)"
    > >
    > >
    > > Note that it should be "horseman (4)" not "horse (4)man".

    >
    > my $n = 0;
    > s/(\w*horse\w*)/do { $n ++; "$1 ($n)"}/eg;


    Thanks!

    --
    Sandman[.net]
     
    Sandman, Aug 3, 2004
    #3
  4. Sandman

    J. Romano Guest

    Sandman <> wrote in message news:<>...
    >
    > Let's say I have a string that goes like this:
    >
    > "A horse is a horse is a horse, on a horseman"
    >
    > Now, I would like add a number to each word that contains
    > the string horse, and this is where I am lost.
    > Basically, what I want to output is this:
    >
    > "A horse (1) is a horse (2) is a horse (3), on a horseman (4)"
    >
    > Note that it should be "horseman (4)" not "horse (4)man".
    > Anyone got a juicy regexp for this? :)


    Yes! You can use the /e and /g switches with s// to make a nice
    elegant substitution:

    $_ = "A horse is a horse is a horse, on a horseman";
    $n = 0;
    s/\b(horse\w*)/"$1 (" . ++$n .")"/ge;

    The s///ge line will find all words that begin with "horse" and
    replace them with "[horse-word] (n)" (where n is a number that keeps
    increasing).

    The /e switch is there so that the substitution expression is
    evaluated before being substituted in the string. The /g switch
    exists so that the substitution happens more than just once.

    -- Jean-Luc
     
    J. Romano, Aug 3, 2004
    #4
  5. Sandman

    Uri Guttman Guest

    Re: Counting occurances of string A in string B, and adding it tostring B

    >>>>> "JR" == J Romano <> writes:

    JR> Sandman <> wrote in message news:<>...
    JR> Yes! You can use the /e and /g switches with s// to make a nice
    JR> elegant substitution:

    JR> $_ = "A horse is a horse is a horse, on a horseman";
    JR> $n = 0;
    JR> s/\b(horse\w*)/"$1 (" . ++$n .")"/ge;

    since you can put as much code as you want with /e (which uses the last
    value of the code block), make it easier to read:

    s/\b(horse\w*)/$n++ ; "$1 ($n)"/ge;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Aug 3, 2004
    #5
  6. Uri Guttman <> writes:

    > >>>>> "JR" == J Romano <> writes:

    >
    > JR> Sandman <> wrote in message news:<>...
    > JR> Yes! You can use the /e and /g switches with s// to make a nice
    > JR> elegant substitution:
    >
    > JR> $_ = "A horse is a horse is a horse, on a horseman";
    > JR> $n = 0;
    > JR> s/\b(horse\w*)/"$1 (" . ++$n .")"/ge;
    >
    > since you can put as much code as you want with /e (which uses the last
    > value of the code block), make it easier to read:
    >
    > s/\b(horse\w*)/$n++ ; "$1 ($n)"/ge;


    TIMTOWDI

    s/\b(horse\w*)/$1 (@{[ ++$n ]})/g;

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Aug 3, 2004
    #6
  7. Sandman

    Anno Siegel Guest

    Abigail <> wrote in comp.lang.perl.misc:
    > Anno Siegel (-berlin.de) wrote on MMMCMXC September
    > MCMXCIII in <URL:news:cenrj5$ro6$-Berlin.DE>:



    > $$ my $n = 0;
    > $$ s/(\w*horse\w*)/do { $n ++; "$1 ($n)"}/eg;
    >
    >
    > No need for the 'do { }'.
    >
    > s/(\w*horse\s*)/$n ++; "$1 ($n)"/eg;
    >
    > will do fine.


    You're right. The substitution part in s///e is, in effect, a do-block.
    No need to wrap it in another one.

    Anno
     
    Anno Siegel, Aug 3, 2004
    #7
  8. Sandman

    Anno Siegel Guest

    Brian McCauley <> wrote in comp.lang.perl.misc:
    > Uri Guttman <> writes:


    [...]

    > >
    > > JR> $_ = "A horse is a horse is a horse, on a horseman";
    > > JR> $n = 0;
    > > JR> s/\b(horse\w*)/"$1 (" . ++$n .")"/ge;
    > >
    > > since you can put as much code as you want with /e (which uses the last
    > > value of the code block), make it easier to read:
    > >
    > > s/\b(horse\w*)/$n++ ; "$1 ($n)"/ge;

    >
    > TIMTOWDI
    >
    > s/\b(horse\w*)/$1 (@{[ ++$n ]})/g;


    Ah, that doesn't need /e. The action can also go on the matching side.

    s/\b(horse\w*)(?{ $n ++ })/$1 ($n)/g;

    Anno
     
    Anno Siegel, Aug 3, 2004
    #8
    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. Rodrick Brown

    counting word occurances

    Rodrick Brown, Jun 1, 2005, in forum: Perl
    Replies:
    2
    Views:
    2,826
    J├╝rgen Exner
    Jun 2, 2005
  2. John
    Replies:
    8
    Views:
    397
    Fredrik Lundh
    Mar 11, 2006
  3. Nico Grubert
    Replies:
    1
    Views:
    463
    Pierre Quentel
    Jun 21, 2006
  4. Replies:
    1
    Views:
    681
    =?iso-8859-1?q?Fl=E1vio_Barata?=
    Jul 31, 2006
  5. Rodrick Brown

    counting word occurances

    Rodrick Brown, Jun 3, 2005, in forum: Perl Misc
    Replies:
    22
    Views:
    307
Loading...

Share This Page