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

[ Editor- This was moved from Categorized Questions and Answers and so didn't have any node text (as requested for that section). ]
  • Comment on How do I get a loop to recognize a number between $i and $i+1 (a decimal number)

Replies are listed 'Best First'.
(ar0n) Re: How do I get a loop to recognize a number between $i and $i+1 (a decimal number)
by ar0n (Priest) on Jul 26, 2001 at 11:20 UTC
    Multiply it by a large factor and divide:
    for my $n (0..100) { $n /= 100; # do something with a number between 0.00 and 1.00 }
    or, for something more arbitrary:
    for my $n (437 .. 1287) { $n /= 1000; # do something with a number between .437 and 1.287 }
    update: the more i read the question, the less certain i am i know what you mean... hope this helps, though...

    ar0n ]

      Aloha, Mahalo for your reply. I am just a beginner. What I am trying to write is a script to correct an online quiz. It works fine for questions that have only one value. But I'm trying to get it to recognize that some questions have more than one value (because they may have dropdown boxes) What I thought I would do was give half credit if one of the two drop down boxes were correct. kailua362
Re: How do I get a loop to recognize a number between $i and $i+1 (a decimal number)
by mattr (Curate) on Jul 26, 2001 at 18:00 UTC
    I think you need to take a step back from the problem, and reconsider how you want to represent this data.

    You might retrieve multiple answers to a question from your online form and place them into a string next to each other using the join command so that selecting both A and B might give you $form[5] = "AB".

    You would then loop through the questions checking the length of this string, which is going to be greater than 1 if multiple choices have been selected. Use a regular expression to see if the correct choice is one of those included in the string, with something like

    $ok = $form[$i] =~ /$ans[$i]/; # e.g. $ans[5] is "A" $points += 0.5 if $ok;
    There are other ways to do it of course, for example you could use the index command which returns -1 if it doesn't find the substring for which you are looking. You could also keep the form data in a hash, or request the multiple values to be returned into an array. See the documentation for CGI.pm as described in this node for more information about that.