The first thing you should do is to get the close() calls in the right context...
#!/usr/bin/perl use strict; use warnings; use File::Find; my $DIR = "/tmp/logs/"; find(\&queries, $DIR); sub queries() { if ( -f and /^ftr001/ ) { print "----------------------------------------------\n"; print "Searching File:", $_, "\n"; print "----------------------------------------------\n"; open(UNZIP, "gunzip -dc < $_ |") or die "gunzip: $!"; open(RECORDS, "/tmp/logs/records.txt") or die "Could not open +records file: $!"; my @REC=<RECORDS>; chomp @REC; close RECORDS; while ( <UNZIP> ) { my $EACH_LINE = $_; foreach my $REC_QUERY (@REC) { print "Searching for current record: $REC_QUERY\n"; if ( $EACH_LINE =~ /\($REC_QUERY\)/ ) { print "Querying for record: $REC_QUERY\n"; print "$EACH_LINE\n"; } } } close UNZIP; } }
...in that way you'll be closing the files at the right time i.e. after you've finished with them. Otherwise you'll encounter problems...

After that, if I've understood you correctly, the simplest approach would be to convert the sought after records into an RE and use that against each file. Moreover, there's a marked performance hit by opening the records file once for every directory - c/w once at the outset - something along the lines of the following (untested) snippet...

#!/usr/bin/perl use strict; use warnings; use File::Find; my $DIR = "/tmp/logs/"; find(\&queries, $DIR); open(RECORDS, "/tmp/logs/records.txt") or die "Could not open records +file: $!"; local $/ = undef; # Ignoring binmode as its a text file my $RE = join '|', <RECORDS>; close RECORDS; sub queries() { return unless -f and /^ftr001/; print "----------------------------------------------\n"; print "Searching File:", $_, "\n"; print "----------------------------------------------\n"; open(UNZIP, "gunzip -dc < $_ |") or die "gunzip: $!"; while ( <UNZIP> ) { next unless /$RE/; print $_; } close UNZIP; }

A user level that continues to overstate my experience :-))

In reply to Re: Not sure what I am doing wrong by Bloodnok
in thread Not sure what I am doing wrong by learningperl01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.