...

View Full Version : returning hash difficulties



bazz
04-18-2009, 02:42 AM
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.



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

bazz
04-18-2009, 02:50 AM
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.




while ( my $data = $sth->fetchrow_hashref )
{
my $menu = $data->{menu_name};

$live_menus{$menu} = $menu;
}

print Dumper %live_menus;
return %live_menus;
}


bazz

oesxyl
04-18-2009, 02:51 AM
try this:


$live_menus{$cols[0]}++ if $cols[0] =~ /\S+/;


best regards

bazz
04-18-2009, 04:28 AM
Thanks oesxyl.

That was a help and I got it sorted out.

bazz

KevinADC
04-18-2009, 06:38 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum