student in perl/cgi has asked for the wisdom of the Perl Monks concerning the following question:

I keep getting an error message:

Use of uninitialized value in addition <+> at c05ex1.cgi line 33. Argument "" isn't numeric in array element at c05ex1.cgi line 34.

I have done this just a my text book said to do. What am I missing?

#!/usr/bin/perl #c05ex1.cgi - saves form data to a file, and creates a dynamic #Web page that displays a message and survey statistics print "Content-type: text/html\n\n"; use CGI qw(:standard -debug); use strict; #declare variables my ($game, $commercial, @records); my %game_count = ("Great", 0, "Boring", 0, "None", 0); my @comm_count = (0, 0, 0, 0); #assign input items to variables $game = param('Game'); $commercial = param('Commercial'); #save form data to a file open(OUTFILE, ">>", "c05ex1.txt") or die "Error opening c05ex1.txt. $!, stopped"; print OUTFILE "$game,$commercial\n"; close(OUTFILE); #calculate survey statistics open(INFILE, "<", "c05ex1.txt") or die "Error opening c05ex1.txt. $!, stopped"; @records = <INFILE>; close(INFILE); foreach my $rec (@records) { chomp($rec); ($game, $commercial) = split(/,/, $rec); $game_count{$game} = $game_count{$game} + 1; $comm_count[$commercial] = $comm_count[$commercial] + 1; } #generate HTML acknowledgment print "<HTML><HEAD><TITLE>WKRK-TV</TITLE></HEAD>\n"; print "<BODY>\n"; print "<H2>Thank you for participating in our survey.</H2>\n"; print "<EM><B>What did you think of the Super Bowl game?</EM></B>\n"; print "<TABLE>\n"; print "<TR><TD>It was a great game.</TD> <TD>$game_count{Great}</TD +></TR>\n"; print "<TR><TD>It was a boring game.</TD> <TD>$game_count{Boring}</T +D></TR>\n"; print "<TR><TD>I didn't watch the game.</TD><TD>$game_count{None}</TD> +</TR>\n"; print "</TABLE><BR>\n"; print "<EM><B>Vote for your favorite Super Bowl commercial:</EM></B>\n +"; print "<TABLE>\n"; print "<TR><TD>Budweiser</TD> <TD>$comm_count[0]</TD></TR>\n"; print "<TR><TD>FedEX</TD> <TD>$comm_count[1]</TD></TR>\n"; print "<TR><TD>MasterCard</TD> <TD>$comm_count[2]</TD></TR>\n"; print "<TR><TD>Pepsi</TD> <TD>$comm_count[3]</TD></TR>\n"; print "</TABLE<BR>\n"; print "</BODY></HTML>\n";

When the array was Game and the hash was Commercial it worked. Instructions said to make Commercial the array and the Game the Hash.

Help me please.
student.

20050516 Janitored by Corion: Added formatting

Replies are listed 'Best First'.
Re: help with counters in perl
by ikegami (Patriarch) on May 16, 2005 at 21:16 UTC
    You omitted a crucial element. You didn't specify what input causes the error. What were the values for $game and $commercial when you got the error? I bet $game wasn't one of "Great", "Boring" or "None", and I bet $commercial wasn't a number...
Re: help with counters in perl
by holli (Abbot) on May 16, 2005 at 21:20 UTC
    The problem is that param("Game") does not return one of the keys you initialize your hash %game with. Consider
    use strict; use warnings; #this is better syntax for creating a hash my %game_count = ("Great" => 0, "Boring" => 0, "None" => 0); my $game = "Great"; # no warning $game_count{$game} = $game_count{$game} + 1;
    but
    use strict; use warnings; #this is better syntax for creating a hash my %game_count = ("Great" => 0, "Boring" => 0, "None" => 0); my $game = "Something"; # this yields the warning $game_count{$game} = $game_count{$game} + 1;
    Furthermore you can replace $game_count{$game} = $game_count{$game} + 1; with $game_count{$game}++;


    holli, /regexed monk/
Re: help with counters in perl
by dynamo (Chaplain) on May 16, 2005 at 21:26 UTC
    The problem is that it's not finding the proper values you initialized to 0 in the 'declare variables' section.

    It's complaining about 33 & 34, the last two lines in the following code block (if you exclude the close brace):

    foreach my $rec (@records) { chomp($rec); ($game, $commercial) = split(/,/, $rec); $game_count{$game} = $game_count{$game} + 1; $comm_count[$commercial] = $comm_count[$commercial] + 1; }

    Since I see that you initialized 'Great','Boring', and 'None' in the %game_count hash, and also the first 4 values in the @comm_count array, it must be that it's reading values from your record file that are outside that set of indices. Maybe there are uncapitalized 'great' entries, or perhaps a vote for commercial 10?

    What's the file look like? That's the next step.

      Ok, I have narrowed it down to the array element is picking up the array Budweiser[1] and MasterCard [3]. I use the -w at the Command Prompt and it says;

      <CODE> Vote for your favorite Super Bowl commercial:
      Budweiser 1
      FedEx 0
      MasterCard 1
      Pepsi 0

      But rec file shows this:

      <CODE> None,2 <CODE>

      The hash and the rec file are correct. How do I get the commercial count to execute correctly?

        The tags are < code > and < /code >, not < CODE > ... < CODE >
        Also, if you had the tags correct, then the < strong > ... < /strong > tags would not have worked within the block.

        Update: Have you tried adding the print statements I suggested in the FP?
Re: help with counters in perl
by TheStudent (Scribe) on May 16, 2005 at 23:35 UTC
    Are you sure you are getting the input you think you are? You could probably find out what is wrong with a couple of simple print statements. How about:
    foreach my $rec (@records) { chomp($rec); ($game, $commercial) = split(/,/, $rec); print "\$game: >$game<\n\$commercial: >$commercial<\n"; # <<<< + debug $game_count{$game} = $game_count{$game} + 1; $comm_count[$commercial] = $comm_count[$commercial] + 1; }

      While it's a good idea to use some sort of delimiter around variables in debug output, < and > probably aren't the best first choices for a CGI context . . .