4 Replies - 5162 Views - Last Post: 04 May 2012 - 09:32 AM

#1 wizzard1200  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Scope Issue/Perl Form Processing

Posted 30 April 2012 - 09:44 PM

What I am trying to do is verify that two input files contain equal values. I am writting to a file from the first form and the file is named based on the name entered in the name text box on the first page. The first page is a form containing exam style questions-4 questions where the user can select their enter by selecting one of the bullets a submit button. The logic i've used is open the answers file and the key file, load each file into a seperate array and then compare the contents of each array. What I am having issues with is printing out each answer as either being right or wrong along with the correct answer being printed. I think my logic is correct and I think there is issue with the order things are being processed. The contents of each file are along the lines of 1966 a d e false or some variation of those characters.
#fill items from answer key
open(FILE,"key.txt");
@key=<FILE>;
close FILE;
#write submission to file to answer file and add a space between each character
foreach $item(@user){
	open (FILE,">>$contents{'name'}.txt");
	print FILE $item;
	print FILE " ";
	}
	close FILE;
#load contents from submissions file
open (FILE,"$contents{'name'}.txt");
@submission=<FILE>;
close FILE;
my $i=0;
my $q=1;
for($i=0;$i<@submission;$i++){
	print "<br />Question $q:";
	if ($submission[$i] eq $key[$i]){
	print "Correct";
	}else{
	print "Wrong. The correct answer is $key[$i]";
	}
$q++; 	
 }


The output I receive is:
Question 1:WRONG. --->The correct answer is 1966 False c d

What am I doing wrong or missing or not understanding?

Is This A Good Question/Topic? 0
  • +

Replies To: Scope Issue/Perl Form Processing

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Scope Issue/Perl Form Processing

Posted 01 May 2012 - 06:29 AM

I don't see anything obviously broken in your code. Can you provide a short key.txt and corresponding submission.txt (say, 3-4 lines) along with the desired output?
Was This Post Helpful? 0
  • +
  • -

#3 wizzard1200  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Scope Issue/Perl Form Processing

Posted 01 May 2012 - 08:10 AM

View Postdsherohman, on 01 May 2012 - 06:29 AM, said:

I don't see anything obviously broken in your code. Can you provide a short key.txt and corresponding submission.txt (say, 3-4 lines) along with the desired output?

The contents of key.txt are 1966 False c d

The desired output would be:

Question 1:Correct
Question 2:Wrong. Correct answer is [input answer from user]
Question 3:Correct
Question 4:Correct

What I am getting instead doesn't make sense. It's as if the entire key array is being unloaded when an answer is correct. The if condition is only being evaluated one time when it should be evaluated through each iteration of the loop.
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Scope Issue/Perl Form Processing

Posted 02 May 2012 - 04:11 AM

View Postwizzard1200, on 01 May 2012 - 04:10 PM, said:

The contents of key.txt are 1966 False c d


Is that all on one line in the input file?

"@key=<FILE>" reads the file line-by-line, so if all the answers are on one line, then @key will only get a single value ("1966 False c d"), not four separate answers ("1966", "False", "c", "d").

View Postwizzard1200, on 01 May 2012 - 04:10 PM, said:

What I am getting instead doesn't make sense. It's as if the entire key array is being unloaded when an answer is correct. The if condition is only being evaluated one time when it should be evaluated through each iteration of the loop.


That would be consistent with having all the answers on a single line in key.txt, as that would lead the program to believe that there is only one question and the correct answer to that question is "1966 False c d".

The best way to fix this would be to change key.txt to put each answer on a separate line:
1966
False
c
d
This solution would not require any changes to your code.

Another option would be to replace your code for reading in key.txt with
my @key;
open my $key, '<', 'key.txt' or die "Failed to open key file: $!";
while (my $line = <$key>) {
  push @key, split ' ', $line;
}
close $key;
This approach has the disadvantage that, because it's using spaces as the separator between answers, you can no longer have answers with spaces in them (unless you make it a lot more complicated or use a module such as Text::Balanced to support spaces in quoted strings).

Even if you fix key.txt to only have one answer per line (again, recommended!), I would advise changing the code which reads it to:
open my $key, '<', 'key.txt' or die "Failed to open key file: $!";
my @key = <$key>;
close $key;
and making similar revisions to the code which reads the submissions file. The way you're using "open" is an older style which is now considered less secure and less maintainable; you can google "perl three-argument open" and "perl lexical filehandles" for more details.
Was This Post Helpful? 2
  • +
  • -

#5 wizzard1200  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Scope Issue/Perl Form Processing

Posted 04 May 2012 - 09:32 AM

View Postdsherohman, on 02 May 2012 - 04:11 AM, said:

View Postwizzard1200, on 01 May 2012 - 04:10 PM, said:

The contents of key.txt are 1966 False c d


Is that all on one line in the input file?

"@key=<FILE>" reads the file line-by-line, so if all the answers are on one line, then @key will only get a single value ("1966 False c d"), not four separate answers ("1966", "False", "c", "d").

View Postwizzard1200, on 01 May 2012 - 04:10 PM, said:

What I am getting instead doesn't make sense. It's as if the entire key array is being unloaded when an answer is correct. The if condition is only being evaluated one time when it should be evaluated through each iteration of the loop.


That would be consistent with having all the answers on a single line in key.txt, as that would lead the program to believe that there is only one question and the correct answer to that question is "1966 False c d".

The best way to fix this would be to change key.txt to put each answer on a separate line:
1966
False
c
d
This solution would not require any changes to your code.

Another option would be to replace your code for reading in key.txt with
my @key;
open my $key, '<', 'key.txt' or die "Failed to open key file: $!";
while (my $line = <$key>) {
  push @key, split ' ', $line;
}
close $key;
This approach has the disadvantage that, because it's using spaces as the separator between answers, you can no longer have answers with spaces in them (unless you make it a lot more complicated or use a module such as Text::Balanced to support spaces in quoted strings).

Even if you fix key.txt to only have one answer per line (again, recommended!), I would advise changing the code which reads it to:
open my $key, '<', 'key.txt' or die "Failed to open key file: $!";
my @key = <$key>;
close $key;
and making similar revisions to the code which reads the submissions file. The way you're using "open" is an older style which is now considered less secure and less maintainable; you can google "perl three-argument open" and "perl lexical filehandles" for more details.


Thank you for your help. This topic has been solved thanks to you. This is also why I will never list Perl as a language that I actually know on my resume lol but again thank you everyone for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1