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

S

Sandman

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? :)
 
A

Anno Siegel

Sandman said:
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
 
J

J. Romano

Sandman said:
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
 
U

Uri Guttman

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
 
B

Brian McCauley

Uri Guttman said:
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\\
 
A

Anno Siegel

Abigail said:
Anno Siegel ([email protected]) wrote on MMMCMXC September
MCMXCIII in <URL:

$$ 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
 
A

Anno Siegel

Brian McCauley said:
[...]
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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top