WARNING: Untested code follows.
#---- find-match.pl------ #!/usr/bin/perl use strict; use warnings; my %match_files = ( m1 => 'match', m2 => 'nomatch0', m3 => 'nomatch1', ); my %match_fh; for (keys %match_files) { open my $fh, '>', $match_files{$_} or die "Can't open $match_files{$_}: $!\n"; $match_fh{$_} = $fh; } while (<>) { my $match_result = test_for_match($_); # whatever it is! my $fh = $match_fh{$match_result}; unless ($fh) { # well, just in case warn "File $ARGV at line $. returns unexpected match result: $matc +h_result. Skipped\n"; # or put to another file? next; } print $fh; close(ARGV) if eof(ARGV); }
Execution,
$ perl find-match.pl file0 file1 fileN
The diamond operator (<>) reads files one line by one line into memory instead of reading the whole content at once. The $_ default variable will hold the line in each iteration. This operator will continue to do so until all files are out of lines, returns undef and the while loop terminates.

The last line in the loop is merely for an aesthetic to allow us identify the correct line number of the currently processed file for throwing warning. You might not need both the last line and the unless clause.

After reading this, you may want to go to at least one of open, perlop, perlvar, and some of perlfaqs. But I think you should go there first though. Simple or super search on the term "read file" will result in a bunch of nodes discussing this issue. Also, you may want to dig a bit on the Q&A part, specially on the files category.

Well, I guess I'm in the mood of something.


Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: need help reading through two large files and output matches and non matches by naikonta
in thread need help reading through two large files and output matches and non matches by jlctx

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.