Reputation: 9636
I want to count the number of characters after I have figured out the starting point.
__DATA__
1-thisthestartingpoint
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
COUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINEBEFORETHENEXTSTARTINGPOINT
2-nextstartingpoint
ETCETCETCETCDONOTCOUNTTHENUMBEROFCHARACTERSPRESENTINTHISLINE
I have written the following script but it does not seem to solve the purpose. it does not go to the line whose characters are supposed to be counted, instead gives length of 1-thisisthestartpoint. Any suggestions on how count the number of characters in the line following the regex. I am new to Perl and programming in general, so kindly go easy on me.
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";
}
Upvotes: 0
Views: 1993
Reputation: 212248
This will count the characters in the startingpoint tags as well:
#!/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
Upvotes: 1
Reputation: 46187
Call me crazy, but this question seems tailor-made for the 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
(Note that this version counts both the starting and ending markers, returning a total of 248 characters. Implementing a conditional within the body of the flip-flop to skip over them is left as an exercise for the reader, as this case is so tailor-made for flip-flop that I can't shake the feeling that it may be someone's homework.)
Upvotes: 0
Reputation: 26121
There is not still clarification what program should do thus there is yet another implementation of something what author may be don't want.
#!/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
Upvotes: 0
Reputation: 342373
while (<>) {
chomp; # strip record separator
if (/^2/) { print $t; last; }
if (/^1/) { $f = 1; }
if ($f) { $t += length($_); }
}
then above code will print total.. if you want to print each line's total, print it in the third "if" block
output
# ./test.pl file
229
Upvotes: 0
Reputation: 118128
This is a little cumbersome but maybe you or someone else can get rid of the need for the print after the loop:
#!/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
Output:
E:\Temp> d 207 characters since 1-thisthestartingpoint 60 characters since 2-nextstartingpoint
Upvotes: 0
Reputation: 140728
This is sort of a contrived answer, but the question is (IMO) worded so oddly that I'm not sure I understand the point here...
#!/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
and output:
$ ./foo.pl
the length is 209
Upvotes: 1
Reputation: 804
i m no perl wizard, but you need to do next in your loop so it start counting after the startpoint. the length calculated is +1 of nextline character.
#!/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";
}
}
Upvotes: 0