Solved

Parse/traverse nested HASH

Posted on 2006-06-23
7
716 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
Independent Software Vendors: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl strange behaviour 5 74
pattern matching in perl 2 111
Perl efficient DB Call 8 96
Removing file extension within a file. 4 102
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

713 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