Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found