MrCromeDome has asked for the wisdom of the Perl Monks concerning the following question:

Bad, bad title, I know ;) (Not sure what happened the last time I tried to post this. . .) I'm trying to publish stats for our hockey league on a web page. Early in the program, I load a hash with the names of each team, and an abbreviation for each. For example:
Abv Team ------------------ WSH Washington BUF Buffalo ANA Anaheim CMB Columbus NJ New Jersey ATL Atlanta MTL Montreal
To make sure I loaded the hash correctly, I used the following code to dump the contents of the hash to a page:
for my $key (keys %teams) { print $key . " - " . $teams{$key} . "<BR>\n"; }
Where %teams is obviously the list of teams, where the abbreviation is the key and the name of the team is the value. The above code did what it was intended - it printed a list of all 30 teams in the league.

The next step was to read through a comma-delimited file (using DBI and DBD::CSV) with the desired stats (what's above and below this works fine):

while (my $row = $sql->fetchrow_hashref) { my %row_data; $team = $row->{'Teams'}; $row_data{TEAM} = $teams{$team}; $row_data{GP} = $row->{'GP'}; $row_data{W} = $row->{'W'}; $row_data{L} = $row->{'L'}; $row_data{T} = $row->{'T'}; $row_data{OTL} = $row->{'OTL'}; $row_data{P} = $row->{'P'}; push(@table, \%row_data); }
@table is an array I hand off to HTML::Template to build a result table. The table prints as intended, however, all of the team names are blank. But if I change this:
$row_data{TEAM} = $teams{$team};
to this:
$row_data{TEAM} = $team;
I get the abbreviation for the team I intended. Even stranger, if I change the line to this:
$row_data{TEAM} = $teams{"NYR"};
I get NY Rangers to show up for all 30 teams. Why are my hash values coming back blank?

I tried doing a super-search for the above problem and didn't seem to turn up anything. Can anybody shed some light on this? It's really frustrating me. I had to put down perl for too long a time (how dare work interfere with something fun ;) and thought I'd get back into the swing of things with something easy.

Thanks in advance,
MrCromeDome

Replies are listed 'Best First'.
Re: Need help hashing this out. . .
by dreadpiratepeter (Priest) on Feb 13, 2002 at 19:22 UTC
    Have you printetd out the value of team? Do it with a delim around it ie print "($team)\n";.
    My guess is that the records are coming back with trailing whitespace, or that the capitalization is different. But probably the whitespace. (Which you can trim with: $team =~ s/^\s*(.*?)\s*$/$1/; among other ways).

    -pete
    Entropy is not what is used to be.
Re: Need help hashing this out. . .
by trs80 (Priest) on Feb 13, 2002 at 20:53 UTC
    What does $row->{'Teams'} contain? The other rows are all upper case and you didn't mention what value is there. My guess is that the $row->{'Teams'} is not the value you are expecting.
      My bad - completely! $row->('Teams') gave me the value I thought I expected (the team abbreviation), however some of the values came in with trailing spaces. . . the regex from dreadpiratepeter helped me catch that.

      Thank you both!
      MrCromeDome