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
.
First, use
pop
to separate the value to assign from the keys. Then, you can use either of the following:or
dive_val
works by having$p
reference the next value to dereference and/or modify.The extra level of indirection has many benefits.
dive_val
to support mixed array/hash structures.