as mentioned above use seek so you don't re read the log. Either qr/ / a series of regex into an array and for it or if you can swiftly split out the IP from the log i.e. if they all apear in the same position on a line you can use unpack to extract the IP and something like this may help. Even more so if your desired IPs cluster a bit in the class A octet
#!/usr/bin/perl
use strict;
use warnings;
$>++;
# get 500 random ip adresses, see genip code below
my %need;
open IP, "./genip |" or die "ooeps $!\n";
for (1..500) {
my ($a, $b, $c, $d)=split /\./, <IP>;
$need{$a}{$b}{$c}{$d}++;
}
for (1..10_000_000) {
my $ip=<IP>;
my ($a, $b, $c, $d)=split /\./, $ip;
# the compiler may optimise this line ...
# next unless exists $need{$a}{$b}{$c}{$d};
# so all the following can probably be replaced
# but it is too late for me to benchmark, g'night
next unless exists $need{$a};
next unless exists $need{$a}{$b};
next unless exists $need{$a}{$b}{$c};
print "a.b.c\n"; # see how sparse we are !
next unless exists $need{$a}{$b}{$c}{$d};
print "match ! $ip\n";
}
close IP;
__END__
# Random IP address generator used above ...
#!/usr/bin/perl
use strict;
use warnings;
while (1) {
my $ip = int rand 256;
for (1..3) {
$ip.= "." . int rand 256;
}
print $ip, $/;
}
To get any hits I upped the searched for IPs to 5000 then saw a few in reaonable time
Cheers,
R.
Pereant, qui ante nos nostra dixerunt!