moon36 has asked for the wisdom of the Perl Monks concerning the following question:

So this is a sample of the data set I am working with:

Frame: 11 Dist: 3.9167561146736 Pair: C3 3.758637 0.476092 -0.993460000000001 C3 0 0 0 C5 -1.869251 0.768024 1.474533 C6 -2.190815 0.66446 -1.008485 C3 3.758637 0.476092 -0.993460000000001 C5 5.761172 1.640144 -0.0484530000000003 C6 4.954926 -0.500627 0.974567 Frame: 12 Dist: 3.53249125062413 Pair: C3 3.525636 0.219958 0.00192600000000098 C3 0 0 0 C5 -1.36273 0.727707000000001 1.967642 C6 -2.447943 0.437720000000001 -0.272385999999999 C3 3.525636 0.219958 0.00192600000000098 C5 5.570011 1.364399 0.87885 C6 4.824893 -0.811070999999998 1.874659

I need to extract the number of lines containing 'C' following each line containing 'Frame'. I have absolutely no idea how to begin coding this (I am super new to perl).

I am hoping to get an output that looks something like: Frame: 11 7 (number of lines containing C)

Help?

Replies are listed 'Best First'.
Re: Counting lines matching a certain pattern following a select word
by Laurent_R (Canon) on Oct 29, 2013 at 21:39 UTC

    This is not a code writing service. I'll just give you the central part of a possible algorithm and leave it to you to fill the gaps (opening the files, closing them, etc.). This is just the basic idea, untested:

    my ($current_frame, $count); while (<$INFILE>) { if (/^(Frame:\s+\d+)/) { print "$current_frame $count \n" if defined $current_frame; $current_frame = $1; $count = 0; elsif (/^C\d+) { $count ++; } } print "$current_frame $count \n"; # prints last group
Re: Counting lines matching a certain pattern following a select word
by Kenosis (Priest) on Oct 30, 2013 at 02:06 UTC

    Hi moon36 and welcome to PerlMonks!

    I have absolutely no idea how to begin coding this (I am super new to perl).

    There are several ways this can be done, but always begin your scripts with:

    use strict; use warnings;

    These pragmas will alert you to problems in your scripts which you may otherwise spend hours trying to figure out. Use them unfailingly.