Regex and multiples on same line

G

Gary Mayor

Hi,
Thanks for everyone in my earlier message. Now i've got a problem with
regex. I've got a file with lines like this,

CATETITLE1 CATETITLE2 CATETITLE3 CATETITLE4
CATETITLE5 CATETITLE6 CATETITLE7 CATETITLE8

while(<FILE>) {
if ($_=~ /CATETITLE/) {
$counter++;
}
}

if I then run through the file and match each time there is a CATETITLE
I only get a count of 2. How do I find out the total amount of CATETITLE?

Thanks

Gary
 
A

A. Sinan Unur

Hi,
Thanks for everyone in my earlier message. Now i've got a problem with
regex. I've got a file with lines like this,

Your problem is relates not to the regex itself, but the matching
operator. Do read perldoc perlop.
CATETITLE1 CATETITLE2 CATETITLE3 CATETITLE4
CATETITLE5 CATETITLE6 CATETITLE7 CATETITLE8

while(<FILE>) {
if ($_=~ /CATETITLE/) {
$counter++;
}
}

Please post code that we can run with no effort.

C:\Home> cat hababa.pl
use strict;
use warnings;

my $counter = 0;
while(<DATA>) {
$counter += (my @m = $_ =~ /(CATETITLE)/g);
}
print $counter, "\n";

__DATA__
CATETITLE1 CATETITLE2 CATETITLE3 CATETITLE4
CATETITLE5 CATETITLE6 CATETITLE7 CATETITLE8

C:\Home> perl hababa.pl
8
 
J

John W. Krahn

Gary said:
Thanks for everyone in my earlier message. Now i've got a problem with
regex. I've got a file with lines like this,

CATETITLE1 CATETITLE2 CATETITLE3 CATETITLE4
CATETITLE5 CATETITLE6 CATETITLE7 CATETITLE8

while(<FILE>) {
if ($_=~ /CATETITLE/) {
$counter++;
}
}

if I then run through the file and match each time there is a CATETITLE
I only get a count of 2. How do I find out the total amount of CATETITLE?


while ( <FILE> ) {
$counter += () = /CATETITLE/g;
}



John
 
A

Anno Siegel

Gary Mayor said:
Hi,
Thanks for everyone in my earlier message. Now i've got a problem with
regex. I've got a file with lines like this,

CATETITLE1 CATETITLE2 CATETITLE3 CATETITLE4
CATETITLE5 CATETITLE6 CATETITLE7 CATETITLE8

while(<FILE>) {
if ($_=~ /CATETITLE/) {
$counter++;
}
}

if I then run through the file and match each time there is a CATETITLE
I only get a count of 2. How do I find out the total amount of CATETITLE?

Lots of ways... Here's another, based on counting the words in each
line that contain "CATETITLE":

while(<FILE>) {
$counter += grep /CATETITLE/, split;
}
}

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

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top