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
    I'll give this a try. I was trying to be elegant and do things faster than brute forcing my way line by line, but I guess you see where that got me... :-(

    Yea, I know I really should have been using strict and i felt like an idiot posting the code without it (thus the warning up top). Thanks for your input!

    when I clean up the code (and am using strict like I should be), I will repost the code.

    perl -e 'print reverse qw/o b n a e s/;'