Got this:
my @list = <one two three>;
my %hash;
my $item1 = @list.shift;
%hash{$item1} = {$item1 => 1};
my $item2 = @list.shift;
%hash{$item1} = {$item2 => 1};
my $item3 = @list.shift;
%hash{$item1}{$item2} = {$item3 => 1};
say %hash;
Outputs this desired data structure:
{one => {two => {three => 1}}}
Obviously, this would be better if it were recursive, so I wrote this:
sub get-hash(%parent-hash, $last-item, *@list) {
my $item = @list.shift;
%parent-hash{$last-item} = { $item => 1 };
get-hash(%parent-hash{$last-item}, $item, @list) if @list;
return %parent-hash<root>;
}
%hash = get-hash({}, 'root', @list2);
Output:
{one => {two => {three => 1}}}
Though it works, it feels inelegant, especially having to pass in a root argument to the sub and then removing it. Any suggestions?