in reply to Re: Match function not providing results
in thread Match function not providing results

I think I'm catching on. I tried the expression =~ /ad/ with better results. However, I now get a correct answer if I check a or ab or ac. I also get incorrect answers if I check bd or cd. What I'm ultimately going to do is have a variable which will contain the answer. For example, $answer might contain a,d (as seen in the text file). I was hoping to be able to compare param('sX') with $answer to make sure the person selected the correct answers. Therefore, I want to make sure I get a true response only if param('sX') contains all the characters in $answer, not just one. Is there a better way of accomplishing this without having to use m//? I tried using if(param('s1') eq "ad" but that didn't work either as I suspect I'm not understanding how param() returns the value. Thanks
  • Comment on Re: Re: Match function not providing results

Replies are listed 'Best First'.
Re: Re: Re: Match function not providing results
by Corion (Patriarch) on Dec 14, 2001 at 21:38 UTC

    I guess you mean /[ad]/. If we look at what I wrote above, /[ad]/ will match any string that contains either an a or a d, which is not yet exactly what you want.

    My advice is that you don't need regular expressions at all, since the set of correct answers can be constructed as a string, for example ad, and the set of given answers can be concatenated to a string as well. Checking whether an answer is correct in total is then just a matter of comparing two strings for equality. But first, we need to take a look at the CGI documentation for checkboxes. The example there tells us how to get the checked boxes, so now all we got to do is to concatenate these into one string and compare them against the correct answer :

    my @selected_answers = param('s1'); my $correct_answer = join( "", @selected_answers ); if ($given_answer ne $correct_answer) { print "Bleh ! You lose !" } else { print "Yay ! Correct !" }

    A small side note - you are not using the strict module. This is very bad and will hide many programming errors from your eyes. So please use strict; !

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web