[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Omaha.pm] Pulling data back out of a hash of arrays.
On Wed, 28 Sep 2005, Dave Thacker wrote:
> Code snippet:
>
> while ( @fixture_rec = $sth->fetchrow_array ) {
> my $game_id = $fixture_rec[0];
> print "0=$fixture_rec[0] 1=$fixture_rec[1] 2=$fixture_rec[2]\n";
> my @teams = @fixture_rec[1..2];
> print "home=$teams[0] away=$teams[1]\n";
> #stuff the hash
> $fixture{$game_id} = [ @teams ];
> }
>
> return;
> }
>
> sub get_teamsheets {
> while (($game_id, @teams) = each %fixture) {
> print "Game=$game_id Home=$teams[0]\n";
> }
> return;
> }
Looks like you're confusing yourself in get_teamsheets(). You think you're
pulling an array of elements into @teams, but you're not. You're pulling a
single element into @teams -- an array reference.
Try this instead and see if it works:
sub get_teamsheets {
while (($game_id, $teamsref) = each %fixture) {
print "Game=$game_id Home=$teamsref->[0]\n";
}
return;
}
- You also probably want to pass a %fixture reference into the sub, not
use a global %fixture. It'll be easier to support your code down the road
that way. Globals quickly get out of control.
- "return;" at the end of a sub doesn't do anything. It just tells perl to
do what it's about to do anyway. You can leave it out or, preferably, do
an explicit return of a true value (like "return 1;") so code calling your
sub can understand that the sub was successful if it happens to care
(which it probably should).
HTH,
j