Reading a file multiple times is usually a "code smell" (a bad coding practice). Very often, as in your case, it's because you want to compare all the items in a file against another group of items. In most cases that gives two options:
- Get the first group into some data structure (most often a hash or array) then process the second group (usually the lines in a file) one at a time and process them using the preloaded first group values
- The first option with group one and two swapped
Usually the expected size of the two groups determines which option to choose. Store the smaller of the two groups of data in memory. Commonly the data structure will be an array or a hash, although in your case a regular expression as suggested by Haukex is probably the best option.
The reason for avoiding rereading a file is that doing so is slow. Reading data from a file is likely to be thousands of times slower than "reading" the same data from memory. With modern computers reading even large files into memory is practical.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.