Nested Foreach and Closure

From: mt35 (mt35_at_linuxmail.org)
Date: 05/29/04


Date: 29 May 2004 06:53:07 -0700

Hello,

I'm trying to pass a lexical variable down from one subroutine to
another. I have read perlfaq7 on closure (among others) and grasp the
concept for the situation they gave, however I think I'm trying to do
something else entirely. Also I'm only two weeks old in perl so if I'm
going about this in totally the wrong way pointers on the right way to
go are appreciated.

I have an array of an ipfilter log (@fwlog) and a separate array of 10
addresses (@hirate), each of which will have an entry in the ipfilter
log more than 15 times. I want to make an additional array (@hirate1)
of the full log entry foreach address, thus weeding out all entries
that do not correspond to the 10 address array (@hirate).

The original code with no closure performed on it:

foreach (@hirate) {
 $y = $_;
  foreach (@fwlog) {
   push (@hirate1, $_) if ($_ =~ $y);
  }
}

Example Variables:
$y = 192.168.1.81
@fwlog = Jan 4 03:01:02 host0 ipmon[51]: 03:01:01.088578 xl0 @0:1 b
192.168.1.81,1047 -> 192.168.1.100,53 PR udp len 20 61 OUT

The original problem I was having was @hirate1 would come out with a
count of approx. 3000 when the original firewall log had a count of
approx 1900.

Here is what I tried:

foreach (@hirate) {
 $y = $_;
 $q = max($y);
 &$q();

  sub max {
   return sub {
    foreach (@fwlog) {
      push (@hirate1, $_) if ($_ =~ $y);
   }
  }
 }
}

However, this brings back approx 3000 entires as well? Do I have some
syntax wrong or am I way off point?

Thanks,

-SLM



Relevant Pages

  • Moving a function from VB to VC
    ... This server is quite big and complex and one ... The algorithm is heavily modified to return ... An array of nodes(approx size 7000) ...
    (microsoft.public.vc.language)
  • Moving a function from VB to VC
    ... This server is quite big and complex and one ... The algorithm is heavily modified to return ... An array of nodes(approx size 7000) ...
    (microsoft.public.vb.winapi)
  • Creating a checksum for a text file
    ... I have a file containing approx 300 lines of text.. ... Each line has a CrLf at the end. ... I am reading strings into an array. ... I need a simple method of creating a checksum for the file. ...
    (comp.lang.basic.visual.misc)