Solved

Parse/traverse nested HASH

Posted on 2006-06-23
7
707 Views
Last Modified: 2008-03-10
I have following problem, i want one max two subs that call it self as long the HASH tree isn't ended, i have written following code:

#!/usr/bin/perl
#use strict;
use Scalar::Util qw(reftype);

my %hello;
$hello{'a1'}->{'a2'}->{'attr1'} = "abc1";
$hello{'a1'}->{'a2'}->{'attr2'} = "abc2";
$hello{'b1'}->{'b2'}->{'attr1'} = "bar1";
$hello{'b1'}->{'b2'}->{'attr2'} = "bar2";
$hello{'b1'}->{'b2'}->{'attr3'} = "bar3";

my $nk;
my $nnk;
my %nh;
my $nh;
&testen($nk);
my $counter = 0;
sub testen{
    $counter++;
    print "Count:$counter\n";
    if(!$nk eq ''){
      %nh = %{$nh{$nk}};
    }else{
      %nh = %hello;
    }
    foreach(keys %nh)
    {
      my $kk;
      $kk = $_;
      if(reftype($nh{$_}) eq 'HASH'){
          for(keys %{$nh{$_}})
          {
            print "KK:$kk S:$_\n";
            $nk = $kk;
            testen($counter, $nk, %nh);
          }
      }else{
          print "Value: $nh{$_}\n";
      }
    }
}
0
Comment
Question by:pcl99
7 Comments
 
LVL 10

Expert Comment

by:xanius
ID: 16968462
pcl99,

it would be nice if you could tell us what your code is supposed to do/producde, then it is nuch easier to make a suggestion.

Cheers
Xanius
0
 

Author Comment

by:pcl99
ID: 16969578
I want to return the keys, but the HASH example can be different from time to time because it's gonna be input data from another source.

it could also look like this:
$hello{'a1'}->{'a2'}->{'attr1'}->{'facs'} = "abc1";
$hello{'a1'}->{'a2'}->{'attr2'}->{'tt'}->{'yyy'} = "abc2";
$hello{'b1'}->{'b2'}->{'attr1'} = "bar1";
$hello{'b1'}->{'b2'}->{'attr2'} = "bar2";
$hello{'b1'}->{'b2'}->{'attr3'} = "bar3";
$hello{'c1'} = "Test";

I need the values from it...
to design a new HASH where other values will be added, but it's here i have the problem.

Cheers
pcl99
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16970905
you want  the names of the keys (a1, b1 etc) or the values of the hash keys (abc1, abc2, etc)?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:pcl99
ID: 16971746
I got the solution myself, see here:

#!/usr/bin/perl
use strict;
use Scalar::Util qw(reftype);

my %hello;
$hello{'a1'}->{'a2'}->{'attr1'} = "abc1";
$hello{'a1'}->{'a2'}->{'attr2'} = "abc2";
$hello{'b1'}->{'b2'}->{'attr1'} = "bar1";
$hello{'b1'}->{'b2'}->{'attr2'} = "bar2";
$hello{'b1'}->{'b2'}->{'attr3'} = "bar3";

testen(\%hello);

sub testen{
    my $hRef = shift || return 0;
    my $level = shift || 0;
    $level++;
    return 0 if $level > 50;
    if (reftype($hRef) eq 'HASH') {
      foreach my $k (keys %{$hRef}) {
          print "Key:$k\n";
          testen($hRef->{$k}, $level);
      }
    }else{
      print "Value:$hRef\n";
    }
}
0
 
LVL 10

Expert Comment

by:xanius
ID: 17316615
Fine with me!
Xanius
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 17347263
PAQed with points refunded (250)

CetusMOD
Community Support Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now