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

Hi Monks,

Is there any book which gives idea about complicated data structures such as producing hashes of hashes of hashes...etc.

Even though i have worked in perl for past 6 months this is the first time handling complicated data structures.

My work is to convert,

a b c e f g h i

the levels are identified by the number of space in front of the text.

I want to get the output 'h' when i print $var->{e}->{f}->{g};

Any suggestions are welcome.

Many thanks in advance.

Replies are listed 'Best First'.
Re: complicated data structures
by ctilmes (Vicar) on Aug 06, 2004 at 10:50 UTC
Re: complicated data structures
by deibyz (Hermit) on Aug 06, 2004 at 11:06 UTC
    Advanced Perl Programming: The first two chapters are a very good introduction to complex data structures. You can also find some other good chapters about some different topics.

    Mastering Algorithms with Perl: Not only data structures (chapters 2 and 3), but almost everything you need to deal with them.

    About your example, getting "h" for $var->{e}->{f}->{g}; is as easy as:

    my $var; $var->{e}->{f}->{g} = "h";
    You can search "autovivification" to know why this works.
Re: complicated data structures
by hmerrill (Friar) on Aug 06, 2004 at 11:20 UTC
    The "Perl Cookbook" has a whole chapter on hashes, and is an excellent book I might add, organized by *what* you want to accomplish! I would also recommend "Programming Perl" as a great reference book.
Re: complicated data structures
by dsb (Chaplain) on Aug 06, 2004 at 11:40 UTC
    As ctilmes said, perldsc is a good (?:free)? resource. But look at perlref first unless you already understand references.


    dsb
    This @ISA my cool %SIG
Re: complicated data structures
by davido (Cardinal) on Aug 06, 2004 at 15:29 UTC

    The relevant POD's, included with every complete distribution of Perl:

    That should keep you busy for a couple of hours, after which, you'll feel at first like you were struck in the head by a phone book and told to memorize it. But a couple hours later, a lightbulb will flick on, and you'll get it. (teasing, of course -- but these are very informative reads)


    Dave

Re: complicated data structures
by pingo (Hermit) on Aug 06, 2004 at 13:54 UTC
    One way to solve your particular problem would be to use eval. Not sure it is the most _elegant_ way, but it would probably be fairly easy.