in reply to Scan ARP cache dump - memory hog
If you're concerned about memory usage, you shouldn't read the entire file at once. Read it line by line and count the IPs in a hash, so you don't get duplicated entries. And, of course, use strict, but I guess you knew that already :) Anyhow, here's a snipped that came to my mind:
#!/usr/bin/perl -w use strict; my $subnet = '192.168.87'; my %data = (); # precompile regex for performance.. my $regex = qr#^($subnet\.\d+)#; # read file line by line while (my $line = <DATA>) { chomp($line); if ($line =~ $regex) { $data{$1}++; } elsif ($debug) { print STDERR "Didn't match: $line\n"; } }
If you really do want to stick to your own code, your line is better written as (it's not very nice either..):
my @matches = grep { m/$regex/ } @lines; ($_) = m/$regex/ foreach @matches;
Regards,
-octo-
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Scan ARP cache dump - memory hog
by seanbo (Chaplain) on Jul 04, 2002 at 02:57 UTC |