Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    sorting split filename into hash - probs

    Hi,

    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?

    Bazz
    Code:
    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};
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,872
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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?

  • #3
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$category}{$business};
    Perhaps you meant this to be...
    Code:
    my ($business, $businessType, $category, $localRegion, $county, $grid, @accomm_info) = split /_/, $dirs{$county}{$business};
    EDIT: FishMonger beat me to it.
    Last edited by Jeff Mott; 07-26-2005 at 06:17 PM.

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    Code:
    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.

    Bazz

    PS Just had a thought....

    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};
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,872
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Maybe reading this might help.
    http://www.unix.org.ua/orelly/perl/prog/ch04_07.htm

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •