Solved

Parse/traverse nested HASH

Posted on 2006-06-23
7
709 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting to linux server using Net::Telnet module 25 220
Port 80 requests 16 86
Perl modules on linux ec2 3 95
How to generate square thumbnail using perl 13 77
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

911 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

19 Experts available now in Live!

Get 1:1 Help Now