in reply to map into a multidimensional hash

If there's always just one id_2 per id_1, you can use
my %hash = map +($_->{id_1} => { $_->{id_2} => $_->{v} }), @aoh;

Otherwise, you need to assign to the inner hash not to overwrite a previous branch.

Tried with

my @aoh = ( { id_1 => "F1", id_2 => "F2", v => "FV" }, { id_1 => "S1", id_2 => "S2", v => "SV" }, { id_1 => "T1", id_2 => "T2", v => "TV" }, { id_1 => "T1", id_2 => "T3", v => "TW" }, );

Another approach would be to use Data::Diver:

use Data::Diver qw{ DiveVal }; my %hash; DiveVal(\%hash, @$_{qw{ id_1 id_2 }}) = $_->{v} for @aoh;

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: map into a multidimensional hash
by Eily (Monsignor) on Mar 09, 2017 at 12:40 UTC

    Wow, I completly failed to see that the issue was with multiple elements sharing the same id_1 with different id_2. In which case the map solution isn't just harder to read, it's also incorrect. I'd ++ multiple times if I could.

Re^2: map into a multidimensional hash
by LanX (Saint) on Mar 09, 2017 at 20:09 UTC
    >   DiveVal(\%hash, @$_{qw{ id_1 id_2 }}) = $_->{v} for @aoh;

    Well you are not deleting the ids from the $_ hash and assigning the value instead of $_.

    I'm also not sure why the hash slice works, most probably fresh syntax, I'd probably prefer listing the two values explicitly.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!