Any problem that basically says, have I seen this element or how many times have I seen this is ideally suited for a hash.
# Change
while (<FILE>) {
/(.*)\s-\s-/;
$ip = $1;
if (notInList($ip))
{
$i++;
addToList($ip);
}
}
# To
my %IPs_Seen = ();
while (<FILE>) {
# Add 1 to the key $1 (The match)
$IPs_Seen{$1} ++ if m/^(.*)\s-\s-/;
}
#later
print "\nVisits for $file is ",scalar(keys %IPs_Seen),"\n";
# To access the ips found.
my @ips = keys %IPs_Seen;
This leaves out a host of other issues but it's a first step. #1 Visitors and IPs don't have a 1 to 1 mapping (proxies/AOL/etc). It will probably save you a ALOT of subroutine calls and the scanning of the @iplist on a file that big though.
What if you have a line that doesn't start with a valid ip? There are probably many other issues as well. That aside, there are many log parsing packages that are already out there. No need to reinvent the wheel unless of course you want the experience or want something different. Re-inventing wheels (or trying to) can be a great learning experience.
-Lee
"To be civilized is to deny one's nature."
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.