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

Hey Monks,

Need some help again, I can't seem to think this through. I had asked for some help a while ago and some awesome monks lent me a hand, but my question wasn't very clear so I'll try again.

I've got a bunch of data in the __DATA__ in the following format:

__DATA__

globalkey:key2:key3:key4:ARRAY value

globalkey:key2:key3:key4:key5:ARRAY value

globalkey:key2:key3:key4:key5:any arbitrary number of keys:ARRAY value

I'm looking for a way to build: $h->{$h1}->{$h2}->{arbitrary number of keys} = [ ]

What's the easiest way to build this?

Thanks for any help!!

Replies are listed 'Best First'.
Re: Need help in building a HofHof...ofA
by BrowserUk (Patriarch) on May 12, 2009 at 04:08 UTC

    Something like this?

    #! perl -sw use 5.010; use strict; use List::Util qw[ reduce ]; $a = $b; ## disable Name "main::b" used only once: possible typo use Data::Dump qw[ pp ]; my %hash = map{ %{ +reduce{ +{ $b => ref( $a ) ? $a : [ split ' ', $a ] } } reverse split ':', $_ } } <DATA>; pp \%hash; __DATA__ gkey01:key1:1 2 3 4 5 6 7 8 gkey02:key1:key2:key3:1 2 3 4 gkey03:key1:key2:key3:key4:1 2 3 4 5 6 7 8 gkey04:key1:key2:key3:1 2 gkey05:key1:key2:key3:key4:1 2 gkey06:key1:key2:key3:key4:key5:1 2 3 gkey07:key1:key2:1 2 3 4 5 6 7 8 9 gkey08:key1:key2:key3:key4:1 2 3 4 5 6 7 gkey09:key1:1 2 3 4 5 6 7 gkey10:key1:1 2 3 4 5 6 gkey11:key1:1 2 3 4 gkey12:key1:key2:1 2 3 4 5 6 7 8 9 10 gkey13:key1:key2:key3:key4:1 2 3 4 gkey14:key1:key2:key3:1 2 gkey15:key1:1 2 3 4 5 6 7 gkey16:key1:key2:1 gkey17:key1:key2:key3:1 2 3 4 5 6 7 8 9 10 gkey18:key1:key2:1 2 3 4 5 6 7 gkey19:key1:key2:key3:1 2 3 4 5 6 7 8 gkey20:key1:key2:1 2 3 4 5 6 7 8 9

    Output:

    C:\test>junk3 { gkey01 => { key1 => [1, 2, 3, 4, 5, 6, 7, 8] }, gkey02 => { key1 => { key2 => { key3 => [1, 2, 3, 4] } } }, gkey03 => { key1 => { key2 => { key3 => { key4 => [1, 2, 3, 4, 5, 6, 7, 8] } +} } }, gkey04 => { key1 => { key2 => { key3 => [1, 2] } } }, gkey05 => { key1 => { key2 => { key3 => { key4 => [1, 2] } } } }, gkey06 => { key1 => { key2 => { key3 => { key4 => { key5 => [1, 2, 3] } } } } + }, gkey07 => { key1 => { key2 => [1, 2, 3, 4, 5, 6, 7, 8, 9] } }, gkey08 => { key1 => { key2 => { key3 => { key4 => [1, 2, 3, 4, 5, 6, 7] } } } + }, gkey09 => { key1 => [1, 2, 3, 4, 5, 6, 7] }, gkey10 => { key1 => [1, 2, 3, 4, 5, 6] }, gkey11 => { key1 => [1, 2, 3, 4] }, gkey12 => { key1 => { key2 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } }, gkey13 => { key1 => { key2 => { key3 => { key4 => [1, 2, 3, 4] } } } + }, gkey14 => { key1 => { key2 => { key3 => [1, 2] } } }, gkey15 => { key1 => [1, 2, 3, 4, 5, 6, 7] }, gkey16 => { key1 => { key2 => [1] } }, gkey17 => { key1 => { key2 => { key3 => [1, 2, 3, 4, 5, 6, 7, 8, 9, +10] } } }, gkey18 => { key1 => { key2 => [1, 2, 3, 4, 5, 6, 7] } }, gkey19 => { key1 => { key2 => { key3 => [1, 2, 3, 4, 5, 6, 7, 8] } } + }, gkey20 => { key1 => { key2 => [1, 2, 3, 4, 5, 6, 7, 8, 9] } }, }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Exactly!! THANKS FOR THE HELP!!!
Re: Need help in building a HofHof...ofA
by GrandFather (Saint) on May 12, 2009 at 04:11 UTC

    Go re-read the answers you got to Need some help building a data structure.. You haven't told us enough about the problem you are trying to solve.

    Based on the information you've given so far, there is no easy way to build what you seem to be asking for because, for example, ...{key4} can't hold both a hash reference and an array reference. How do you want to handle that?

    But really, tell us what the bigger problem is you are trying to solve. I suspect you've latched on to trying to solving an implementation detail where you should be revising an issue with the larger problem.


    True laziness is hard work
Re: Need help in building a HofHof...ofA
by ikegami (Patriarch) on May 12, 2009 at 05:50 UTC
    Stop repeating yourself and tell us what's wrong with the previously supplied solutions