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
947 views
in Technique[技术] by (71.8m points)

perl - Create a multidimesional key of hash from array?

I want to create a multidimensional %hash from the @array.

Suppose @array is like

my @array=(1,2,3,4,5);

I want to assign @array last value as final value to multidimensional %hash i.e

%hash=(
        1=>{ 
              2=>
                 {
                  3=>
                      {
                        4=>5
                       }
                  }
              }
           )

Which means $hash{1}{2}{3}{4}=5;

I want to do it in something like:

for my $i (0..$#array){
    #push $i as key until second last element and assign last element as value
} 

Note : The @array may be of any size, Just I want to assign last element of @array as value to the keys of elements before the last element in %hash.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

First, use pop to separate the value to assign from the keys. Then, you can use either of the following:

use Data::Diver qw( DiveVal );

my %hash;
DiveVal(\%hash, map $_, @keys) = $val;

or

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

my %hash;
dive_val(\%hash, @keys) = $val;

dive_val works by having $p reference the next value to dereference and/or modify.

Pre-loop:            $p references $hash (the anon scalar referencing %hash)
After loop pass 0:   $p references $hash->{1}
After loop pass 1:   $p references $hash->{1}{2}
After loop pass 2:   $p references $hash->{1}{2}{3}
After loop pass 3:   $p references $hash->{1}{2}{3}{4}

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 dive_val 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

...