您的位置:首页 > 其它

Count number of times matched

2014-05-08 09:39 260 查看
Count number of times
matched

Sample Code:

__Begin__

use strict;

my $export_dir = 'C:/learnperl/export';

my $format_dir = $export_dir.'/format';

my $process_dir = $export_dir.'/process';

my $validation_dir = $export_dir.'/validation';

my $target = 'ProductMultiplier';

my $output_dir;

mkdir($export_dir, 0777) if(! -d $export_dir);

mkdir($format_dir, 0777) if(! -d $format_dir);

mkdir($process_dir, 0777) if(! -d $process_dir);

mkdir($validation_dir, 0777) if(! -d $validation_dir);

open(FH,"<a.txt")||die "Cannot open the file:
a.txt";

while(<FH>){

chomp;

if(/^\s*Category/
&& /\\\\($target)/){

print;

my @matches =
/\\\\([^\\^"]+)/g;

# This is used only when you
want to count matches.

#my $count =
()=/\\\\([^\\^"]+)/g;

# This is the match output
count.

#print "matched: ".scalar
@matches."\n";

print "Match result:
@matches\n";

# Get the last folder name in
category

my $folderName =
$matches[$#matches];

print "Last folder is:
$folderName.\n";

if($folderName eq
'format'){

print "This
is a format job.\n";

}

elsif($folderName eq
'process'){

print "This
is a process job.\n";

}

elsif($folderName eq
'validation'){

print "This
is a validation job.\n";

}

else{

print "This
is a main job or parameter set.\n";

}

}

}

my $line = "This is a sentence about matching sentences.\n";

my $count = 0;

while( $line =~ /(sentence\w*)/ig ){

my $lexical = $1;

$count ++;

print "$count. $lexical\n";

}

print "\ttotal: $count\n";

__End__

If I want to count the number of
times that a match occurs in a line is there a way of doing this,
if there is I have not found it!

e.g.

$line="This is a sentence about matching sentences.\n";

$line=~/sentence/ig;

So I will have matched "sentence"
twice and I want to record this.

I can do it like this:

$line=~/sentence/ig;

# count the number of instances of
this in the sentence

@split=split(/sentence/,$line);

$length= @split;

$length--;

This code works fine, but if I need
to keep each matched term (as I have wild-cards to capture spelling
variants).

i.e. output would be:

sentence

sentence

Is there a way to automatically
capture each individual match (e.g. using a special array
character?)

--------------------------------------

Is this close to what you want?

#!/usr/bin/perl

use strict;

use warnings;

my $line = "This is a sentence
about matching sentences.\n";

my $count = 0;

while( $line =~ /(sentence\w*)/ig ){

my $lexical = $1;

$count ++;

print "$count. $lexical\n";

}

print "\ttotal: $count\n";

__END__

No, you haven't. A pattern match
in scalar context only matches ONCE.

What you want to do is:

my @matches = $line =~
/($pattern)/ig;

Assigning the return value of the
pattern match to an array means that the

pattern match is executed in LIST context (instead of scalar
context), so

it will match as many times as it can (due to the /g
modifier).

-------------------------

If you just want to count
occurrences you can use a counter:

my $c = 0;

++$c while $line =~ /sentence/g;

There's a shorter idiom that takes
advantage of the way list

assignments work in scalar context that involves something known as
the "goatse operator" (that's jargon it's not a real
operator):

my $c =()= $line =~
/sentence/g;

but you have to choose which one
is more easy to understand for you.

I don't know whether this is what you
ask for, but m//g in list context returns the captures in one
shot:

my @tags = $html =~
/<(\w+)/g;

That is documented in
perlop.

-- fxn

> I want to count
the number of times that a match occurs

perldoc -q count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: