Seamus Carr wrote: > > I'm trying to count first occurrence of a pattern in a block of lines > which have the same number in first field. I was using if statements to > test the conditions. The problem is that it reads the pattern of every > line, not skipping rest of the block if the pattern has been matched. > Putting the block of related lines into an array is impractical due to > the length of the lines in the file. I tried nesting an if conditional, > but it didn't make any difference in skipping the lines in the block. > > #!/usr/local/bin/perl -w > use strict; > my $count; > my $control; > $count=0; > while (my $lfile = glob"ZKL*.ARC"){ > $lfile =~ /^ZKL(\d+).ARC/; > open(LFILE, "<$lfile"); > while (<LFILE>) { > my $control = substr($_,0,4); > if ($control < 9000){ > if (m/PATTERN/){ > ++$count; > if ($control != substr($_,0,4)){ > my $control = substr($_,0,4); > }else{ > next; > } > } > } > } > } > } > print "$count\n"; > > small snippet of file1 > 230 11 0 2510 1219ZZ1 735L >all lines continue until 250 > 1230 11 1 1160 1220ALM >characters > 1230 11 2 8120 1221J K > > 1232 11 0 1111 1221J K > > 1232 11 1 1111 1222J K >
It looks like you are trying to count the number of lines that contain PATTERN from the beginning of the file to the first line that starts with 9000. If so then this will do what you want: #!/usr/local/bin/perl -w use strict; my $count; @ARGV = glob 'ZKL*.ARC'; while ( <> ) { close ARGV if /^9000/; $count++ if /PATTERN/; } print "$count\n"; __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]