Hi Seamus 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.
'Count the first occurrence'? That would be '1' ;-) > 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. You need a conditional 'last' statement to skip the remaining lines in the current file. > > #!/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"; Hi Seamus I got a bit lost amongst all those braces; I suspect you did too! I'm not exactly sure what your check is supposed to be, but I think you're counting the number of files which match /ZKL\d+\.ARC/ and which contain at leaast one line starting with an integer less than 9000 and containing /PATTERN/? If not, correct me. Try this: #!/usr/local/bin/perl -w use strict; my $count; foreach (glob "ZKL*.ARC") { next unless /^ZKL(\d+)\.ARC/; open LFILE, "< $_" or die "Couldn't open $_: $!"; while (<LFILE>) { /^(\d+)/; if ($1 < 9000 && m/PATTERN/) { ++$count; last; } } close LFILE; } print "$count\n"; __END__ HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]