I have an array and I am making a hash instance from it.
For instance, if array is:
@folders=(temp,usr,bin);
then i want to fill in hash:
$the_path{$folders[0]}{$folders[1]}{$folders[2]}="somevalue";
But if the array is only:
@folders=(bin);
then i want the path to be:
$the_path{$folders[0]}="somevalue";
The problem is I dont know beforehand how long the array is gonna be, and I would really like to avoid making x if statements for that solution scales terribly.
How do I do this?
First, that’s not how you define an array in Perl. You probably want to say
There’s an old trick for making nested hash keys from a list:
This will result in a structure like the following:
If you want to replace the empty hashref at the bottom-most level with a string, you can keep track of a count variable inside the loop.