[Omaha.pm] Pulling data back out of a hash of arrays.

Dave Thacker dthacker9 at cox.net
Wed Sep 28 21:14:38 PDT 2005


On Wednesday 28 September 2005 06:59, Jay Hannah wrote:
> 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;
> }
The result is now. 
"Unrecognized character \xC2 at ./auto-run.pl line 83." 
I'm not at all sure where that is coming from...

>
> - 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.

Perhaps it is already?
>
> - "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).

I can fix that.  
> HTH,
>
> j



More information about the Omaha-pm mailing list