我想在计算出起点后计算字符数。
__DATA__
1-thisthestartingpoint
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
我编写了以下脚本,但它似乎没有解决目的。它不会转到其字符应该被计数的行,而是给出 1-thisisthestartpoint 的长度。关于如何计算正则表达式后面的行中的字符数的任何建议。我是Perl的新手和一般的编程,所以请对我很轻松。
open (FILE, "/usr/filename") || die "cant open filename";
my @body = <FILE>;
foreach $_(@body){
last if ($_=~/[2-9]-[a-z]+/);
if ($_=~ /1-[a-z]+/){
chomp ($_);
push (@value ,split (//,$_));
my $length = @value;
print @value;
print "\n the length is $length\n";
}
答案 0 :(得分:1)
这是一个人为的答案,但问题是(IMO)措辞奇怪,以至于我不确定我理解这一点......
#!/usr/bin/perl
use strict;
use warnings;
chomp( my @lines = <DATA> );
my $data = join '' , @lines;
my( $string ) = $data =~ /1-[a-z]+(.*)[2-9]-[a-z]+/;
printf "the length is %d\n" , length( $string );
__DATA__
1-thisthestartingpoint COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT 2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
并输出:
$ ./foo.pl
the length is 209
答案 1 :(得分:1)
这也会计算起点标签中的字符:
#!/usr/bin/env perl
use strict;
use warnings;
my $count;
while ( <DATA> ) {
$count += length if m'thisthestartingpoint' .. m'nextstartingpoint';
}
print "count: $count\n";
__DATA__
1-thisthestartingpoint
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
答案 2 :(得分:0)
我没有perl向导,但你需要在你的循环中做下一步,所以它在起始点之后开始计数。计算的长度是下一个字符的+1。
#!/usr/bin/perl
open (FILE, "./abc") || die "cant open filename";
my @body = <FILE>;
foreach $_(@body){
last if ($_=~/[2-9]-[a-z]+/);
if ($_=~ /1-[a-z]+/) { $found = 1; next; };
if ($found == 1)
{
chomp ($_);
push (@value ,split (//,$_));
my $length = @value;
print @value;
print "\n the length is $length\n";
}
}
答案 3 :(得分:0)
这有点麻烦,但也许你或其他人可以摆脱循环后打印的需要:
#!/usr/bin/perl
use strict;
use warnings;
my ($length, $marker);
while ( my $line = <DATA> ) {
chomp $line; # decide if you need this
if ( $line =~ /^([0-9]-\w+)/ ) {
if ( $marker ) {
print "$length characters since $marker\n";
}
$marker = $1;
$length = 0;
next;
}
$length += length $line;
}
print "$length characters since $marker\n";
__DATA__
1-thisthestartingpoint
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
输出:
E:\Temp> d 207 characters since 1-thisthestartingpoint 60 characters since 2-nextstartingpoint
答案 4 :(得分:0)
while (<>) {
chomp; # strip record separator
if (/^2/) { print $t; last; }
if (/^1/) { $f = 1; }
if ($f) { $t += length($_); }
}
然后上面的代码将打印总数..如果你想打印每一行的总数,请在第三个“if”块中打印
输出
# ./test.pl file
229
答案 5 :(得分:0)
还没有澄清程序应该做什么,因此还有另一种作者可能不想要的实现。
#!/usr/bin/env perl
use strict;
use warnings;
my ( $mark, $length );
while (<DATA>) {
if (/^([0-9]-\w+)/) {
print "$length after $mark" if $mark;
( $mark, $length ) = $_;
next;
}
chomp; # may be
$length += length;
}
print "$length after $mark" if $mark;
__DATA__
1-thisthestartingpoint
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
答案 6 :(得分:0)
叫我疯了,但这个问题似乎是为flip-flop operator定制的:
#!/usr/bin/perl
use strict;
use warnings;
my $count;
while (<DATA>) {
if (/^1-[a-z]/ .. /^[2-9]-[a-z]/) {
chomp;
$count += length $_;
}
}
print "$count characters between markers\n";
__DATA__
SKIPTHISSKIPTHISSKIPTHIS
1-thisthestartingpoint
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
(请注意,此版本同时计算起始和结束标记,总共返回248个字符。在触发器的主体内实现条件以跳过它们留给读者,因为这种情况是所以为触发器量身定制的,我无法感觉它可能是某人的作业。)