Recurse over a hash without knowing any elements (except top level)

From: Christopher (decryption69_at_yahoo.com)
Date: 02/11/04


Date: 11 Feb 2004 13:55:17 -0800

I have a hash that is several levels deep.

ie:

'vars' => {
                      '$filers' => '10.10.10.10/32',
                      '$dmz_networks' => '10.10.10.10/32',
                      '$vpn_networks' => '10.50.0.0/16',
                      '$security_wintel_boxes' => '10.10.10.10/32',
          },
'match' => {
                      'RuleResource' => {
                            'ITOC' => {
                               'RuleSubResource' => {
                                 'VIRUS' => {

Basically a structure similar in nature to that, basically it could go
as deep as 8 keys deep with values potentially with each key, but
usually a value as some level.

I need to iterate over this and print it out in it's structure, but
with some formatting. DataDumper does a fine job of doing it in it's
way, but I cannot customize the pretty output, plus I need to utilize
the data in other ways.

So I figure I can load the "top level" keys (say there are 3 of them),
and then recurse over each subsequent key and gather and print as I
go.

Here is what I tried:

use Config::General;

my $match_file = qq(match.conf);

my $match_conf = new Config::General(
                -file => $match_file,
                -AutoTrue => 1);

my %match_config = $match_conf->getall;

$depth = 0;

foreach $key (keys %match_config) {
   print "\t"x$depth;
   print "\n$key\n";
   $depth++;
   &recurse_hash($match_config{$key},$depth);
}

sub recurse_hash {

  $match = shift;
  $depth = shift;
 
  print "\t"x$depth;

  foreach $key2 (keys %{$match}) {
      print "$key2 => $match->{$key2}\n";
      ($match,$depth) = &recurse_hash($match->{$key2},$depth);
  }
  return ($match,$depth);
}

I get some results back, but I also get some funk, plus I don't get it
all, just portions..... I have racked my brain on this, and scoured
the groups for anykind of similar problem. Most are only 3 levels
deep, and the levels are known.

Here is the results:
Top level keys are typically gonna be (vars, match, and translate):
vars
        $filers =>
        $dmz_networks =>
        $vpn_networks =>
        $security_wintel_boxes =>
        $filer_admin =>
        $wireless_networks =>
        $ras_networks =>
        $my_networks =>
        $vnc_networks =>
                
match
                RuleResource => HASH(0x17a918)
                ITOC => HASH(0x17a924)
                RuleSubResource => HASH(0x17a99c)
                VIRUS => HASH(0x17a9a8)
                mail_body => blah blahblah
                mail_cc =>
                page_cc =>
                interval =>
                GUN =>
                DOS =>
                PN =>
                srcnet =>
                                
translate
                        CT2 => HASH(0x183a44)
                        27-74 => HASH(0x183abc)
                        msg_pattern => HASH(0x183b04)
                        ".*filename: => (.*)" = "Bad file: \1"
                        27-75 =>
                        SN =>
                        MSG =>



Relevant Pages

  • Recurse over a hash without knowing any elements (except top level)
    ... I have a hash that is several levels deep. ... Top level keys are typically gonna be (vars, match, and translate): ...
    (comp.lang.perl)
  • Re: How do I handle an unknown number of keys to hash?
    ... accessing DEEP keys will autovivify higher level keys ... but nothing will autovivify when doing a top level access. ... the "deep" keys. ... I was thinking about the "deep" access behavior. ...
    (comp.lang.perl.misc)
  • [PATCH] Dell inspiron multimedia keys need to be assigned keycodes
    ... multimedia keys which worked fine in v2.4, ... What I found (which may be new to some Dell users who are not familiar ... In 2.4, under X, the keyboard was in RAW mode, and the server got the ... am not sure these are the best keycodes to translate to, ...
    (Linux-Kernel)
  • Re: 2.6.0_test6: CONFIG_I8K produces wrong/no keycodes for specialbuttons
    ... In 2.4, under X, the keyboard was in RAW mode, and the server got the scancodes and did whatever ... Now, in 2.6, the keyboard ALWAYS translates keys, passes them translated through the input layer, ... and on the other side, if we are under X, it un-translates them to emulate the good old RAW mode. ... first, atkbd.c must be taught how to translate these keys to something ...
    (Linux-Kernel)
  • Re: Decrypting an encrypted file
    ... > surprise there). ... > create keys, add Recovery Agents etc. and throw the file away? ... Let me know if you can translate it, otherways I will see what I can do for ...
    (microsoft.public.windowsxp.security_admin)