in reply to Looping through Multi Dimensional Hashes

I think you've rather shot yourself in the foot with that data structure. It really makes it difficult to access the second level categories that are children of a particular category.

Personally I'd use the flexibility of Perl data structures to my advantage and build a structure like this:

#!/usr/local/bin/perl use strict; use warnings; my %category_hash = ( 1 => { name => 'whatever', children => { 2 => { name => 'something', children => { 3 => { name => 'another subcategory' } } } } }, 4 => { name => 'something else' } );

You can then display the structure with code like this:

show(\%category_hash, 0); sub show { my ($hash, $lvl) = @_; my $prefix = ' ' x $lvl; foreach (sort keys %$hash) { print "$prefix$_ : $hash->{$_}{name}\n"; show($hash->{$_}{children}, ++$lvl) if exists $hash->{$_}{children}; } }

But you should also look at the Tree modules from CPAN.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: Looping through Multi Dimensional Hashes
by CodeJunkie (Monk) on Jul 15, 2003 at 14:11 UTC

    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

      OK, so lets see who we can build up a data structure like mine from your database. I'm going to read it from the DATA filehandle as I don't have time to set up a database, but the logic is just the same.

      #!/usr/local/bin/perl use strict; use warnings; my %category_hash; my %categories; # Build a hash containing all of our data records... while (<DATA>) { chomp; my %cat; @cat{'id','name','parent'} = split /,/; $categories{$cat{id}} = \%cat; } # ... and turn it into a tree foreach $_ (keys %categories) { if ($categories{$_}{parent}) { $categories{$categories{$_}{parent}}{children}{$_} = $categories{$_}; } else { $category_hash{$_} = $categories{$_}; } } # And now %category_hash is exactly the same as it was # in my previous post. Carry on with "show" as before. __DATA__ 1,whatever,0 2,something,1 3,another subcategory,2 4,something else,0
      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        Thanks! That code is complete genius and worked first time! I wish I could vote the same posts 10 times :-)

        For anyone that wants to try the full program check this out:

        #!c:\perl\bin\perl use strict; use warnings; my %category_hash; my %categories; # Build a hash containing all of our data records... while (<DATA>) { chomp; my %cat; @cat{'id','name','parent'} = split /,/; $categories{$cat{id}} = \%cat; } # ... and turn it into a tree foreach $_ (keys %categories) { if ($categories{$_}{parent}) { $categories{$categories{$_}{parent}}{children}{$_} = $categories{$_}; } else { $category_hash{$_} = $categories{$_}; } } # And now %category_hash is exactly the same as it was # in my previous post. Carry on with "show" as before. #You can then display the structure with code like this: show(\%category_hash, 0); sub show { my ($hash, $lvl) = @_; my $prefix = ' ' x $lvl; foreach (sort keys %$hash) { print "$prefix$_ : $hash->{$_}{name}\n"; show($hash->{$_}{children}, ++$lvl) if exists $hash->{$_}{children}; } } __DATA__ 1,whatever,0 2,something,1 3,something,1 4,somethingelse,1 5,another subcategory,2 6,something else,0 7,something else,0

        Thanks again davorg

        Tom