an idea on how to start and stop counting

K

Kevin Lind

The following code is a subset of a much larger input file.
The one of these words: HILOK, RIVET, or DRILL ONLY will be used in
the header line for each section.

I would like to report the count of M60 or M61 for each section thru
the end of the file. In this example, I have three sections. Therefore
I need to report three counts, one for each section.

A line in the HILOK section may or may not end with only a M60.

A line in the RIVET section may or may not end with only a M61.

A line in the DRILL ONLY section may or may not end with only a M60.

There will be more than one HILOK, RIVET, and DRILL ONLY sections in
the input file


(================================)
(= HILOK DRIL AND C`SINK =)
(================================)
N02596X297.8441Y-.5391Z37.5688W10.9181M60
N02597X296.8211Y-.539Z37.4342W10.952A8.218
N02598X296.7717Y.3536Z37.7132W10.8817A8.6908
N02599X297.0447Y-4.3658Z36.2423W11.3566A6.1743M60
N02600X297.7999Y-5.2943Z36.0538W11.4502A5.724M60
N02601X298.6357Y-4.3293Z36.4727W11.2881A6.304
N02619X293.0531Y-4.4513Z35.6179W11.5485
N02620X291.9772Y-4.473Z35.4392W11.6052A5.7539M60
N02621X292.1467Y-5.332Z35.185W11.7236A5.2921M60
N02622X293.1033Y-5.4268Z35.3083W11.6885A5.3107M60
(================================)
(= RIVET C`SNK GL= 6.5 =)
(================================)
N00092X232.6527Y-9.4525Z23.6936W17.9612M61
N00093X232.6212Y-8.5242Z23.9571W17.6523A-1.7173M61
N00094X232.5889Y-7.5956Z24.2229W17.3572A-1.1093M61
N00095X232.5185Y-6.6674Z24.4868W17.0793A-.5034M61
N00096X232.154Y-5.7794Z24.6954W16.8583A.0507M61
N00098X234.7757Y-6.6616Z24.8563W16.8222
N00098X234.7757Y-6.6616Z24.8563W16.8222M61
N00099X234.8428Y-8.5441Z24.3096W17.3977A-1.5186M61
N00100X234.9058Y-10.4251Z23.7793W18.0251A-2.7382M61
N00101X239.3422Y-10.5556Z24.457W17.5416A-2.3904
N00101X239.3422Y-10.5556Z24.457W17.5416A-2.3904
(================================)
(= DRILL ONLY C`SNK GL= 6.5 =)
(================================)
N02596X297.8441Y-.5391Z37.5688W10.9181M60
N02597X296.8211Y-.539Z37.4342W10.952A8.2183
N02598X296.7717Y.3536Z37.7132W10.8817A8.6908
N02599X297.0447Y-4.3658Z36.2423W11.3566A6.1743M60



An example of output could look like this:

(= HILOK DRIL AND C`SINK =): 6
(= RIVET C`SNK GL= 6.5 =): 8
(= DRILL ONLY C`SNK GL= 6.5 =): 2

The only thing I'm looking for is an idea on how to start and stop
counting between section headers.
 
J

Josef Möllers

Kevin said:
The following code is a subset of a much larger input file.
The one of these words: HILOK, RIVET, or DRILL ONLY will be used in
the header line for each section.

I would like to report the count of M60 or M61 for each section thru
the end of the file. In this example, I have three sections. Therefore
I need to report three counts, one for each section.

A line in the HILOK section may or may not end with only a M60.

A line in the RIVET section may or may not end with only a M61.

A line in the DRILL ONLY section may or may not end with only a M60.

There will be more than one HILOK, RIVET, and DRILL ONLY sections in
the input file
The only thing I'm looking for is an idea on how to start and stop
counting between section headers.

Take a look at hashes, they may solve your problem.
 
R

Richard Gration

<SNIP>

This appears to work nicely, producing the output below ...

--------------CODE-------------------
open (INPUT_FILE,'data.txt') or die "Couldn't open input file: $!\n";

my %search_string = (HILOK=>'M60',RIVET=>'M61',DRILL=>'M60');
# stop 'use warnings' bitching about undef $ss before 1st header
my ($ss,%count,$key) = ('uninitialised');

while (<INPUT_FILE>) {
if (/(HILOK|RIVET|DRILL)/) {
chomp($key = $_);
$ss = $search_string{$1};
next;
}
if (/$ss$/) {
$count{$key}++;
}
}
print "$_: $count{$_}\n" foreach (keys %count);

--------------OUTPUT-----------------
(= DRILL ONLY C`SNK GL= 6.5 =): 2
(= HILOK DRIL AND C`SINK =): 6
(= RIVET C`SNK GL= 6.5 =): 8
 
G

Gunnar Hjalmarsson

Kevin said:
I would like to report the count of M60 or M61 for each section
thru the end of the file. In this example, I have three sections.
Therefore I need to report three counts, one for each section.

There will be more than one HILOK, RIVET, and DRILL ONLY sections
in the input file

An example of output could look like this:

(= HILOK DRIL AND C`SINK =): 6
(= RIVET C`SNK GL= 6.5 =): 8
(= DRILL ONLY C`SNK GL= 6.5 =): 2

The only thing I'm looking for is an idea on how to start and stop
counting between section headers.

Use three count variables. This is a hash idea:

my %M60_1;
my $section;
while (<DATA>) {
chomp ($section = $_) if /^\(= /;
$M60_1{$section}++ if /M6[01]$/;
}
print "$_: $M60_1{$_}\n" for keys %M60_1;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top