in reply to Re: Looping through Multi Dimensional Hashes
in thread Looping through Multi Dimensional Hashes
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Looping through Multi Dimensional Hashes
by davorg (Chancellor) on Jul 15, 2003 at 14:58 UTC | |
by CodeJunkie (Monk) on Jul 15, 2003 at 15:53 UTC |