ministry has asked for the wisdom of the Perl Monks concerning the following question:


my issue today is with re-initializing a hash. in the following code (i posted it all, to help minimize the confusion) i use a hash as a counter for every instance i run into a specific pattern match. in this code i look through a firewall config file, and the same firewalls logfile for rule numbers. the config file will yield a result of all rules listed on the firewall, while the logfile search will result in a total count of each rule that it has found.

($rule)="$1"; $hash{$rule}=($hash{$rule}+1);
with this being the only way i know how to increment a count of undefined items, i have not found a way to properly clear this hash before assigning it to the task of searching through my next firewalls config/log file. in searching the web, the only item i have found is the use of %hash=() however this will not eleminate each key/value pair from my hash.
#!/usr/bin/perl $reportdir="/report/csv"; @basedirs=qw</log /log2>; foreach $starting_point (@basedirs){ &recursion("$starting_point"); } sub recursion { my $dir=shift; opendir(DIR, $dir) or return; my @contents=map "$dir/$_",sort grep !/^\.\.?$/,readdir DIR; closedir DIR; foreach (@contents) { next unless !-l && -e; &recursion($_); next if -d | /gz/ | ! /20050412/; if (/12\/(\w+(-\w+)??)(-\d)??-20050412/){push(@list,$1);} } } sub seek{ my $directory=shift; opendir(LIFT, $directory) or return; my @closed=map "$directory/$_",sort grep !/^\.\.?$/,readdir LIFT; closedir LIFT; foreach (@closed){ next unless !-l && -e; &seek($_); next if -d | ! /$indiv/; next if ! /20050412/; push(@cleaned,$_); } } sub gather{ `tar xvf /var/config/$uniqu-config.20050412`; $config_fw=""; open (GATEWAY,"$config_fw") || die "cant open gateway:$!"; while (<GATEWAY>){ if (/\#(\d+)/) { $rule="$1"; $hash{$rule}="0"; } } close GATEWAY; unlink ($config_fw); } %seen=(); @uniqu=grep { ! $seen{$_} ++ } @list; foreach $uniqu (@uniqu){ %hash=(); if ($uniqu=~/tias/){$tune="/log";$indiv="$uniqu-";} else {$tune="/log2";$indiv="$uniqu-";} open(REPORT,">$reportdir/$uniqu-20050412"); &seek($tune); &gather; for $i (@cleaned){ open (FH,"$i"); while (<FH>){ if (/rule\=(\d+)/){ ($rule)="$1"; $hash{$rule}=($hash{$rule}+1); } } } foreach $rule (sort{$a<=>$b} keys(%hash)){ print REPORT "$uniqu,12/04/2005,$rule,$hash{$rule}\n"; } close REPORT; close FH; }
any help or advice (aside from suggesting i re-write the entire code) would be greatly appreciated.
regards, ev

Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.