Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse/traverse nested HASH

Posted on 2006-06-23
7
Medium Priority
?
726 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

721 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