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

Hey Monks,

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

__DATA__

globalkey:key2:key3:key4:ARRAY VALUES

globalkey:key2:key3:key4:key5:ARRAY VALUES

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

So I'd like to build a HofHofH...ARRAYS for all this data. The last item is always an array but I have an arbitrary number of keys. Whats the easiest way to build this? Thanks for any help!!

Replies are listed 'Best First'.
Re: HoH..oA data structure
by ikegami (Patriarch) on May 10, 2009 at 00:45 UTC

    The last item is always an array but I have an arbitrary number of keys

    I'm not clear how an item (singular) can be an array, so what follows may not be exactly what you want, but it should give you a basis from which you can work.

    use Data::Diver qw( DiveRef ); my $data; while (<DATA>) { chomp; my @keys = split /:/; my $val = pop(@keys); my $ref = DiveRef( $data, map \$_, @keys ); push @$ref, $val; }

    Data::Diver

      Wow. I didn't know there was a module that can do a nested data structure.

      I had to make two changes to get this to work: initialize $data and dereference $ref when pushing the "ARRAY VALUES" value.

      Adding the data BrowserUK provided in Re: Need help in building a HofHof...ofA, I ended up with the following:

      use strict; use warnings; use Data::Dumper; use Data::Diver qw( DiveRef ); my $data = {}; while (<DATA>) { chomp; my @keys = split /:/; my $val = pop(@keys); my $ref = DiveRef( $data, map \$_, @keys ); push @$$ref, $val; } print Dumper($data); __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

      After which it produced:

        Did you want [ '1 2 3 4' ] (one element array!?), or [ 1, 2, 3, 4 ]? Fix:
        use Data::Diver qw( DiveRef ); my $data = {}; while (<DATA>) { chomp; my @keys = split /:/; my $val = pop(@keys); my $ref = DiveRef( $data, map \$_, @keys ); $$ref = [ split ' ', $val ]; }
Re: HoH..oA data structure
by AnomalousMonk (Archbishop) on May 10, 2009 at 01:00 UTC
    Please take a look at the approach ikegami has provided and also at the approaches discussed in answer to the startlingly similar question posed in Need some help building a data structure. and tell us how they do or do not serve your purpose. Knowing this, we may be better able to advise on the most effective approach.