In your first example you are scanning the Apache log in its entirety for each department in your list. That's 40 passes.... Fletch's solution will reduce that to a single pass, it **will** be faster.
Your second solution got the log procesesing down to a single pass and then uses memory to hold the data. For "small" logs this will work, but you will run out of memory as the log gets larger. The real solution, as Fletch pointed out, is to write the data once you have determined where it should go into an extract file (one per department). The code Fletch proposes will scale nicely, as you add more departments (another bonus).
As to the amount of time it takes, you said "I have a very large Apache log...". There is a basic Principle of Science to bear in mind here:
TTT -- Things Take Time.
----
I Go Back to Sleep, Now.
OGB
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.