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
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
相关文章推荐
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.
- Oracle&nbsp;OFA&nbsp;简介
- LintCode_249 Count of Smaller Number before itself
- 使用mybatis执行sql的时候为什么会出现Parameter index out of range (1 > number of parameters, which is 0)?
- *Find the Number Occurring Odd Number of Times
- HDOJ&nbsp;&nbsp;1005&nbsp;&nbsp;&nbsp;Number&nbsp;Sequence
- Co-segmentation&nbsp;of&nbsp;3D&nbsp;shapes
- some&nbsp;senior&nbsp;topic&nbsp;of&nbsp;php
- Money is not out of the province
- TheChroniclesOfNarnia I 进度22%
- NumberFormatException&nbsp;.pars…
- LintCode_284 Count of Smaller Number
- [Lintcode]Count of Smaller Number
- resource is out of sync with t…
- PL/SQL--&gt;INSTEAD&nbsp;OF&nbsp;触发器
- Journal of Machine Learning Rese…
- ORA-06553:PLS-306:wrong number or types of arguments in call to ''
- count the number of "1" in the binary figure
- HDOJ&nbsp;&nbsp;1010&nbsp;&nbsp;&nbsp;Tempter&nbsp;of&nbsp;the&nbsp;Bone
- 关于ibatis的parameter index out of range (1 > number parameters which is 0).异常