in reply to Re: Re: Looping through Multi Dimensional Hashes
in thread Looping through Multi Dimensional Hashes
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
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Looping through Multi Dimensional Hashes
by CodeJunkie (Monk) on Jul 15, 2003 at 15:53 UTC |