in reply to Looking Backward While counting

Here one try I hope you can take it as a starting point.

I make the follwing assumptions:

- it is "##closeFlag## (without blank)

- your input is stored in a file called "in.txt"

- the input file is small enough to fit into memory

use strict; # first slurp the file my $content = do { local $/; open my $fh, "<", "in.txt" or die $!; <$fh> }; # then split it into chunks my @chunks = $content =~ /##Flag##(.*?)##CloseFlag##/sg; # now analyze the chunks for my $chunk (@chunks) { my ($first, $last) = $chunk =~ /T:(\d+).*?(\d+)\D*$/s; # count number of newlines my $newlines = $chunk =~ tr/\n/\n/; print "first-last: ", $first - $last, "# of lines: ", $newlines-1, +"\n"; }

Replies are listed 'Best First'.
Re^2: Looking Backward While counting
by johngg (Canon) on Oct 15, 2010 at 23:21 UTC

    Just a very minor point regarding your code. You don't need to tr/x/x/ when just counting characters; tr/x// will do.

    knoppix@Microknoppix:~$ perl -E ' > $str = q{banana}; > say $str; > $cnt = $str =~ tr{a}{}; > say $cnt; > say $str;' banana 3 banana knoppix@Microknoppix:~$

    Cheers,

    JohnGG