Hi,

I'm new to perl but very enthusiastic about it. I'm having an issue
with the following code.

The task:

two arrays one with a key "key" and one with responses from a form
"responses". The idea is that each value in the responses array will
be compared to key; if the values match then a 1 or a 0 is stored in
the third  array. I can't understand why the last element in the score
array is always a zero. I am printing all the elements an the correct
key, response are printed but the score is always zero. However when I
use  arrays "mkey" and "mresponses" the logic work correctly.

I am sure I have several Perl issues please excuse me for that. I am
studing as much as I can to learn the basics. I came from VB6.0.

Thanks in advance for all you help in resolving the problem.

 -Tony

#!/usr/bin/perl -w
# form.pl
use CGI ':standard';

print "Content-type: text/html\n\n";

# GET THE KEYS

# the key should be at program files\perl express\scripts

# OPEN THE KEY

my @key=();
my @score_key=();
sub key{
    my $inFile = 'key.csv';
    open(IN, $inFile) or die "open $inFile: $!";
    @key=split(',',<IN>);
    @score_key=shift(@key);
    return @key;
}

key();
print "<p>key:</p> @key \n\r";

#GET THE QUESTIONS
my @preguntas=(param());
my @questions=grep(/Question_/,@preguntas);
#print "<p>questions:</p> @questions \n\r";

#GET THE RESPONSES ONLY THE TEST ITEMS

my @responses=();
my @vals=();
my @resp=();
my $xname="";

sub responses {
    foreach my $xname(@questions)
     {
     my @vals = param($xname);
     my @resp=push(@responses,@vals);
     #print "responses: @resp \n\n";
    }
    return(@responses);
    return(@resp);
}

# CALL THE RESPONSES

responses();
print "<p>variable=</p> @responses \n\r";

# LOAD THE ARRAY FOR SCORING
my @scores=();
my $totalscore=0;
my $percent=0;
my $mitem=0;


#my @mkey      =('B','B','B','C','C');
#my @mresponses=('B','C','C','A','A');
my $x = 0;
my @score=();
while($x <= 4){
     if ($key[$x] eq $responses[$x]) {
       $score[$x] = 1;
     }
     else
     {
      $score[$x] = 0;
     }
    print "<p>Questions key $x:</p> Key: $key[$x] Your answer:
$responses[$x]    Question score: $score[$x] \n\r";
    $x++;
 }
 print "<p>scores:</p> @score";

the output:


key:

B B B C C
variable=

B B B C C
Questions key 0:

Key: B Your answer: B Question score: 1
Questions key 1:

Key: B Your answer: B Question score: 1
Questions key 2:

Key: B Your answer: B Question score: 1
Questions key 3:

Key: C Your answer: C Question score: 1
Questions key 4:

Key: C Your answer: C Question score: 0
scores:

1 1 1 1 0
total score: 4


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to