in reply to Out of memory inefficient code?
That makes 9 or 10 copies of the list. It's no wonder you're running out of memory.
Try this. It avoids most of those copies:
use strict; use warnings; open(OUTNOMATCH, ">nomatch.out") or die "Couldn't write to file $!"; open(OUTMATCH, ">match.out") or die "Couldn't write to file $!"; sub match_internal { #Separate internal/external addresses use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip ); my $my_ip = $_[0]; my $regexp = create_iprange_regexp( qw( 192.168.0.0/16 10.10.0.0/16 192.3.3.0/23 192.168.24.0/21 10 +.0.0.0/8 ) ); if (match_ip($my_ip, $regexp)) { print OUTMATCH "$my_ip\n"; } else { print OUTNOMATCH "$my_ip\n"; } } sub sortme { # sort all addresses my @array = @_; my %hashTemp = map { $_ => 1 } @array; my @array_out = sort keys %hashTemp; } sub main_loop { #main loop that performs all logic and munging my %uniq; while (<>) { (my $field1, my $field2) = split /DST=/, $_; if ($field2 =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) { $uniq{ $1 } = 1; } } match_internal( $_ ) while $_ = each %uniq; } main_loop();
You're also re-creating this:
my $regexp = create_iprange_regexp( qw( 192.168.0.0/16 10.10.0.0/16 192.3.3.0/23 192.168.24.0/21 10 +.0.0.0/8 ) );
for every uniq IP you check...which probably doesn't cost you in extra memory, but is hugely wasteful of cpu (time).
|
|---|