Thanks for the reply, I think you might have a point ;-) I kinda thought that mid-way through coding...

The main problem is that I am getting values from a database. The table looks like this:

id,name,description,parent_id

Of course I could change this table structure, but I thought it should hold all the values ok. I am then reading from the database like so:

sub getCategories() { my ($dbh)=@_; my $sth=$dbh->prepare("SELECT categories.id, categories.name, categories.description, categories.parent_id FROM categories"); $sth->execute(); my %cat_hash; while (my $cat_hashref = $sth->fetchrow_hashref()) { if ($cat_hashref->{parent_id}) { $cat_hash{$cat_hashref->{id}}{$cat_hashref->{parent_id}}{name}=$ +cat_hashref->{name}; $cat_hash{$cat_hashref->{id}}{$cat_hashref->{parent_id}}{descrip +tion}=$cat_hashref->{description}; $cat_hash{$cat_hashref->{id}}{$cat_hashref->{parent_id}}{parent_ +id}=$cat_hashref->{parent_id}; } else { $cat_hash{$cat_hashref->{id}}{0}{name}=$cat_hashref->{name}; $cat_hash{$cat_hashref->{id}}{0}{description}=$cat_hashref->{des +cription}; $cat_hash{$category_hashref->{id}}{0}{parent_id}='0'; } } $sth->finish(); return %cat_hash; }

Thanks for your help, I will check out the Tree modules, maybe then can shed some light on the situation for me.

Appreciate any comments people might have on the above code.

cheers,
Tom


In reply to Re: Re: Looping through Multi Dimensional Hashes by CodeJunkie
in thread Looping through Multi Dimensional Hashes by CodeJunkie

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.