tags:

views:

336

answers:

7

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";
}
A: 

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";
    }
}
Abu Aqil
+1  A: 

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
genehack
A: 

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
Sinan Ünür
@Sinan: I dont want to count characters in line '1-thisthestartingpoint'. I only want to count characters below the above line which in this case equal to 207 (i.e. 229 - (1-thisthestartingpoint) =207)
birdy
@birdy The fix is trivial (and incorporated above now).
Sinan Ünür
+1  A: 

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
William Pursell
A: 
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
ghostdog74
A: 

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
Hynek -Pichi- Vychodil
A: 

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.)

Dave Sherohman