Here is an example of an oop approach you may find useful.
package component; my %sc; sub new { bless { name => "$_[1]" }, 'component'; } sub name { $_[0]->{name}; } sub sc { $sc{$_[0]->name} = [] unless $sc{$_[0]->name}; push @{$sc{$_[0]->name}}, $_[1]; $_[0]{$_[1]} = new component($_[1]); }; sub label:lvalue { $_[0]->{lbl}; } sub subs { \@{$sc{$_[0]->name}}; } 1; package project; sub new { bless { }, 'project'; } sub components { my @c; @c = sort keys %{$_[0]}; \@c; } sub addcomponent { $_[0]->{$_[1]} = new component( $_[1] ); $_[0]->{$_[1]}; } sub show { my $proj = shift; foreach my $comp ( @{$proj->components} ){ print "$comp\n"; next unless $#{$proj->{$comp}->subs} > 0; foreach my $sc ( @{$proj->{$comp}->subs} ){ next unless $proj->{$comp}{$sc}; print "\t$sc\t" if $sc; print $proj->{$comp}{$sc}->label,"\n" if $proj->{$comp}{$sc}->label; } } } 1; use strict; my $proj = new project(); my $i = 0; while( <DATA> ){ $i++; chomp; next if $i == 1; my ($comp, $subcomp, $label ) = split /\t/, $_; my $sw = $proj->{$comp}; $sw = $proj->addcomponent( $comp ) unless $sw; my $sc = $sw->sc($subcomp); $sc->label = $label; } $proj->show; my $hw = join ', ', @{$proj->{Hardware}->subs}; print "Hardware subcomponents ( $hw )\n"; my $sw = $proj->{Software}; my $db = $sw->{Database}; my $label = $db->label; print "$sw->{name} $db->{name} $label\n"; __DATA__ COMPONENT SUBCOMPONENT LABEL Software Database Test-1.01 Software Good Languages Perl Software Sucky Languages Java Hardware Monitor 19" LCD Hardware CPU Pentium 2.4 Ghz

Cheers,
JamesNC

In reply to Re: Using multi-level hashes by JamesNC
in thread Using multi-level hashes by carcassonne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.