Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

perl - How to create recursive hash of hash ? (With unlimited deep)

Please, I try to create a complexe datastructure. I know how to do

$branch{'level1'}{'level2'}{'level3'}='leaf';

But I don't know how to create

$branch{'level1'}....{'levelN'}='leaf';

I try something like that :

$branch{'leaf'} = "1";
$branchREF = \%branch;
$branchtmp{'level3'} = $branchREF;

So I succefully get :

$VAR1 = 'level3';
$VAR2 = {
          'leaf' => '1'
        };

But for the next step, to do a recrusive N hash of hash, I try :

%branch = %branchtmp;

But the result is completly wrong... %branch is not what I'm expecting. To do my recursivity, I need to reuse my first %branch and not create a new one. How can I do please ?

A.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I strongly recommend using an existing solution such as Data::Diver.

use Data::Diver qw( DiveVal );

my @keys = map "level$_", 1 .. 3;

my $branch = {};
DiveVal($branch, map $_, @keys) = 'leaf';
  -or-
my %branch;
DiveVal(\%branch, map $_, @keys) = 'leaf';

Obviously, it can be done without module too.

sub DiveVal :lvalue {
   my $p = shift;
   $p = ( $$p->{$_} ) for @_;
   $$p
}

my @keys = map "level$_", 1 .. 3;

my $branch;
DiveVal($branch, @keys) = 'leaf';
  -or-
my %branch;
DiveVal(\%branch, @keys) = 'leaf';

How my DiveVal works:

Pre-loop:          $p references $branch
After loop pass 0: $p references $branch->{level1}
After loop pass 1: $p references $branch->{level1}{level2}
After loop pass 2: $p references $branch->{level1}{level2}{level3}
Returned:          $branch->{level1}{level2}{level3}

The extra level of indirection has many benefits.

  • It removes the need to treat the last key specially.
  • It removes the need to create the hash before it's dereferenced.
  • It removes the need for the root to be a reference to a hash. Instead, any scalar can be the root, even an undefined one.
  • It makes it easy to extend DiveVal to support mixed array/hash structures.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...