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

    returning hash difficulties

    Hi,

    I am calling a MySQL table query from a child sub, into a parent sub to use as a hash. I want to compare the hash values with those in an array and if they match, output one thing and if they don;t to output another. The has returns '1' instead of as string and I need you help on fixing it.

    Code:
    show_menus {
      
      my @list_of_available_menus = get_available_menus($business_id,$business_name,$connect);
    
      my %live_menus = get_live_menus($business_id,$business_name,$connect);
    
      foreach my $key ( sort @list_of_available_menus ) 
      {
        print qq( lmk = $live_menus{$key} ); # busted
         if ( $key ne $live_menus{$key} ) 
        {
          print qq(
          <tr>
            <td>
              <input class="checkBox_inactiveMenu" type="checkbox" name="menuName" value="$key" />
            </td>
            <td><a href="$Request/$key">$key</a>
            </td>
          </tr> 
          );
        }
        else
        {
          print qq(
           <tr>
             <td>
               <input class="checkBox_activeMenu" type="checkbox" name="menuName" value="$menuName" checked="checked" />
             </td>
             <td><a href="$Request/$key">$key</a>
             </td>
           </tr>
          );
        }
      }
    
    
    }
    
    
    
    sub get_live_menus {
    
      my ($business_id,$business_name,$connect) = @_;
      my %live_menus;
      my $sth = $connect->prepare("select
                                     dm.menu_name
                                   from
                                   dishes_menus dm
                                   where dm.live_from <= curdate() 
                                   and business_id = ?
                                  ") or die "prepare statement failed: $DBI::errstr\n";
    
      $sth->execute($business_id);
    
      while ( my @cols = $sth->fetchrow_array) {
        $live_menus{$cols[0]}++;
      }
      print Dumper %live_menus;
      return %live_menus;
    
    }
    Dumper shows this:

    $VAR1 = 'Starters'; $VAR2 = { '' => 1 };

    Please can anyone tell me what I am missing?

    bazz
    "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
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Ahh, maybe I should have done a hashref instead?

    is this OK? I have had to build the hash in what I think is a strange way.

    Code:
      while ( my $data = $sth->fetchrow_hashref ) 
      {
      my $menu = $data->{menu_name};
    
      $live_menus{$menu} = $menu;
      }
    
      print Dumper %live_menus;
      return %live_menus;
    }
    bazz
    "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

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    Code:
       $live_menus{$cols[0]}++ if $cols[0] =~ /\S+/;
    best regards
    Last edited by oesxyl; 04-18-2009 at 02:53 AM.

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks oesxyl.

    That was a help and I got it sorted out.

    bazz
    "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
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Ahh, maybe I should have done a hashref instead?
    Thats correct, you have to return a hash reference. The only time you can return a hash is when its just a regular hash with one level of key/value pairs. Any data structure that contains references needs to be returnd as a reference.


  •  

    Posting Permissions

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