Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to re-useing a hash by ministry

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2023-10-03 15:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found