in reply to Generation of a Hash of Hashes

Your existing code hardcodes all of the possible levels. You need to traverse the hash to whatever the appropriate depth is and then place your entry there. Try the following to see how to do it:
#!/usr/bin/perl -w use strict; use Data::Dumper; my %result; for my $l (<DATA>) { my ($cod,$desc) = ($l =~/(\d*) (.*)/); my $hashref = \%result; my $partial = ''; foreach my $layer (split //, $cod) { $partial .= $layer; $hashref = ($hashref->{$partial} ||= {}); } $hashref->{0} = $desc; } print Dumper \%result; __DATA__ 1 ITEM 11 Sub Item 1 111 Item X 112 Item 1121 Another Item 11212 And Another Item 12 Sub Item 2
Note that before your iron down what you want your representation to be, you should try to write sample code to do the tasks that you need to do using this representation. I'm trying to figure out for what task this data structure would be convenient, and I'm having no luck. Combined with your inexperience, I'd take this as a sign that you probably need a different representation but don't know what.

Replies are listed 'Best First'.
Re^2: Generation of a Hash of Hashes
by Miguel (Friar) on Mar 25, 2005 at 23:08 UTC
    "I'm trying to figure out for what task this data structure would be convenient"

    Thanks for your reply.

    For this case, I have a file with information formated as I've posted above.

    This is:
    1 to 5 digits, 1 space, 1 to 65 characters

    Each line represents classes and subclasses (and sub-sub-classes...) and their names.
    Each digit represents one level.

    For example:

    11234 Some Text ^^^^^^^^^---- Name of this class (level) ^------- 5th class ^-------- 4th class ^--------- 3th class ^---------- 2nd class ^----------- Top (main class)

    Classes' levels varies between 1 and 9.

    I'm trying to use this Hash to create an XML document.

      I'd suggest trying to write code to produce the XML document and only then figure out what data structure you need to do it. (There are so many variations in what XML can look like that your task doesn't tell me what a good data structure would be for your problem.)