I was taking inspiration from this post https://stackoverflow.com/a/16157433/3880362. The only thing that it did not do was increment the value of each key as it was populated. I.e.
I have:
$Hash => {
'Val1' => 1,
'Val2' => 1,
'Val3' => 1
};
When I want
$Hash => {
'Val1' => 0,
'Val2' => 1,
'Val3' => 2
};
Code:
$Hash{$_}++ for (@line);
$Hash{$_}=keys %Hash for (@line);perl -MData::Dump -e '@line = qw(apple banana cherry tree); $hash{$line[$_]} = $_ for 0..$#line; END {dd \%hash}' { apple => 0, banana => 1, cherry => 2, tree => 3 }. Otherwise can maybe set up a counter variable, but not sure if you can do it without a loop.