Your approach is one good way to do it if you do a few minor modifications. Read your first file into a hash instead of an array for easier lookup:

my %records; open my $fh, "<", $file1; /^(\S+)\s/ and $records{$1}=1 while <$fh>; close $fh;

I am using a regular expression to find the first column in your file which seems to be simpler here and can also be used as a test. For example, empty lines will be skipped automatically this way.

Then open each of your other files in turn and check whether a given line shall be printed by doing a lookup in the hash:

for my $file (@filelist) { open my $fh01, "<", $file; /^(\S+)\s/ and defined $records{$1} and print while <$fh01>; close $fh01; }

I have skipped all checks whether files can be opened etc. You need to add those yourself.


In reply to Re: how to check if a particular value exist in list of files stored in an array by hdb
in thread how to check if a particular value exist in list of files stored in an array by Perlseeker_1

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.