wizzard1200's Profile User Rating: -----

Reputation: 1 Apprentice
Group:
New Members
Active Posts:
12 (0.01 per day)
Joined:
10-October 11
Profile Views:
442
Last Active:
User is offline Sep 04 2012 05:03 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   wizzard1200 has not set their status

Posts I've Made

  1. In Topic: Scope Issue/Perl Form Processing

    Posted 4 May 2012

    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.
  2. In Topic: Scope Issue/Perl Form Processing

    Posted 1 May 2012

    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.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

wizzard1200 hasn't added any friends yet.

Comments

wizzard1200 has no profile comments yet. Why not say hello?