View Full Version : sorting split filename into hash - probs

07-26-2005, 06:58 PM

I can't seem to get the hang of hashes to do what I need.

Imagine a flyout menu. I am trying to populate it from a hash (or however anyone might suggest), but am slowly becoming exasperated. It is intended to be a four level menu.

Here's what i have so far. If i reduce it to two values eg {county}{$business} it will show values. As it is now, it seems to do nothing. What am I doing wrong?


foreach my $county (sort keys %dirs) {
foreach my $business (sort keys %{ $dirs{$county} }) {
my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$category}{$business};

07-26-2005, 07:11 PM
Where are you getting the $catagory var in
split /_/, $dirs{$county}{$category}{$business}

Based on your foreach loop, the level you're at now should be
split /_/, $dirs{$county}{$business}

But I suspect you want to go 1 level deeper, right?

Jeff Mott
07-26-2005, 07:13 PM
my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$category}{$business};Perhaps you meant this to be...
my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$business};

EDIT: FishMonger beat me to it.

07-26-2005, 07:30 PM
Great Guys, you're up to speed in a shot.

Fishmonger you spotted the 'deliberate' spelling mistake but, that isnt the cause of my difficulty.

I did previously have $dirs{$county}{$business}; but I do want to go to another two levels.

Basically, I want to list 'counties' alphabetically, then the available categories (eg 5star, 4star etc), then businessType (eg hotel, guesthouse etc), and finally the business itself.

What I have tried is

foreach my $county (sort keys %dirs) {
foreach my $category (sort keys %{ $dirs{$county} }) {
foreach my $businessType (sort keys %{ $dirs{$category} }) {
foreach my $business (sort keys %{ $dirs{$businessType} }) {
my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$category}{$businessType}{$business};

but it seems not to work.

Oh and one other thing, I am trying to make them each list alphabetically within their own grouping and I thought sort would do this.


PS Just had a thought.... :eek:

should each 'foreach', put together the previous hash with the one it creates? so as we get through them all we finsih with this hash?

foreach my $business (sort keys %{ $dirs{$county}{$category}{$businessType} }) {
my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$category}{$businessType}{$business};

07-26-2005, 09:24 PM
Maybe reading this might help.

This secion of it might be what you want
4.7.5 More Elaborate Records