in reply to Correct Loop Structure

Interesting interpretations of your question so far. Here's mine:
my %seen; while(<LOG>){ foreach my $i (@newips){ print OUTFILE if /\;drop;[^"]*$i/ && ++$seen{$i} <= 500; } }

Replies are listed 'Best First'.
Re: Re: Correct Loop Structure
by Dru (Hermit) on Jan 29, 2004 at 20:34 UTC
    ysth,

    Yeah that's what I was thinking ;-)

    Your's and arden's code (from his second response) where the only two that I could get to work. I like the simplicity of your's, but arden's seems to run faster.

    I guess this was a bit more tricky then I though. Thank god for perlmonks. I appreciate the help. -Dru
      Your's and arden's code (from his second response) where the only two that I could get to work. I like the simplicity of your's, but arden's seems to run faster.

      Maybe faster. But arden's code, like yours and all the others except ysth's, have a major flaw: they will skip the first (matching) field of your log file!

      If you decide to use arden's solution, change $newIPseen{$i}++ to ++$newIPseen{$i}

      dave

        Well done Not_a_Number++, I totally missed that (of course, since I didn't test it, no wonder!).

        To prove that in perl you can do things more than one way, this would also work:
        ( print OUTFILE if (/\;drop;[^"]*$i/) ) && $newIPseen{$i}++;
            although I do prefer using ++$newIPseen{$i};.

        update: mark this day my friends, I was wrong!*



        *not a day has gone by when arden wasn't wrong about something

      If the performance isn't good enough, you can probably increase it a fair amount by doing something like this (untested):
      my %seen; my %newip_res; for my $ip (@newips) { $seen{$ip} = 0; $newip_res{$ip} = qr/;drop;[^"]*$ip/; } while(<LOG>){ foreach my $ip (@newip) { ++$seen{$ip}, print OUTFILE if $seen{$ip} != 500 && /$newip_res{$i +p}/; } }
      If there are more than a very few lines that don't have ";drop;" in them, also put a  next unless /;drop;/; just before the foreach.