joshpk105 has asked for the wisdom of the Perl Monks concerning the following question:

This is a conceptual data structure problem. I would love to hear the answers if anyone has any ideas.

You have an array of arrays, turn it into a single hash of hashes where the final element of each array is the value and each previous element in order are the keys to get to that value.

Desired effect Example: IN: Array of Arrays: (0=>(a,b,e,d), 1=>(a,b,c,f), 2=>(d,h,f), 3=>(d,i,g)) OUT: Hash: $out{a}{b}{e} = d $out{a}{b}{c} = f $out{d}{h} = f $out{d}{i} = g

Replies are listed 'Best First'.
Re: Complex Hashing
by SuicideJunkie (Vicar) on Oct 28, 2011 at 17:47 UTC

    You'll want references to hashes, and a loop.

    use strict; use warnings; use Data::Dumper; my $hashref = {}; my $hashrefTemp = $hashref; $hashrefTemp = ($hashrefTemp->{a} ||= {}); $hashrefTemp = ($hashrefTemp->{b} ||= {}); $hashrefTemp->{e} = 'd'; print Dumper $hashref;
    $VAR1 = { 'a' => { 'b' => { 'e' => 'd' } } };

    Looping and I/O is left as an exercise for the reader.

    Dealing with input where an endpoint coexists with a non-endpoint is also left as an exercise for the reader. (Eg: [['a','b'] ['a','b','c']])

      I figured it out thank you.

      I have no idea what this segment is doing

      $hashrefTemp->{a} ||= {}
Re: Complex Hashing
by Anonymous Monk on Oct 28, 2011 at 17:51 UTC
    Learn how to notate complex data structures in native Perl code.
    use Data::Diver qw(DiveVal); use Data::Dumper qw(Dumper); my $out = {}; for my $nodes_ref ( [qw(a b e d)], [qw(a b c f)], [qw(d h f)], [qw(d i g)], ) { my $leaf_value = pop @{ $nodes_ref }; DiveVal($out, @{ $nodes_ref }) = $leaf_value; } print Dumper $out; __END__ { a => { b => { c => 'f', e => 'd', } }, d => { h => 'f', i => 'g', }, }