Solved

Parse/traverse nested HASH

Posted on 2006-06-23
7
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

623 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