Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to count matches?

Reply
Thread Tools

how to count matches?

 
 
Geoff Cox
Guest
Posts: n/a
 
      04-28-2005
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
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      04-28-2005
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

 
Reply With Quote
 
 
 
 
Arndt Jonasson
Guest
Posts: n/a
 
      04-28-2005

Geoff Cox <(E-Mail Removed)> 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;
 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      04-28-2005
On 28 Apr 2005 10:48:10 GMT, John Bokma <(E-Mail Removed)> 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.


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      04-28-2005
On 28 Apr 2005 12:51:38 +0200, Arndt Jonasson <(E-Mail Removed)>
wrote:

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


Thanks Arndt - all is well!

Cheers

Geoff

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Count = Count + 1 Using only std_logic_1164 Doubt efelnavarro09 VHDL 2 01-26-2011 03:49 AM
TBird: Unread NewsGroup Message Count Never Accurate Herb Firefox 4 03-29-2005 02:00 AM
Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? Kaimuri MCSD 3 12-29-2004 06:38 PM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments