in reply to Match function not providing results

The reason your regular expressions don't work is that you are using them wrong.

The perlre manpage (use perldoc perlre to view it) gives a short introduction to regular expressions and how to construct them.

Here's a short attempt at a hands-on introduction geared towards your problem :

A regular expression that matches a single character is /f/. This one would match barfoo.

A regular expression that matches a sequence of characters is, for example, /foo/, which would match barfoobaz, but which would not match barfobaz, because the second o is missing.

To now construct a regular expression that matches one character out of a set of characters, we neet to look at the set constructors for regular expressions, [ and ]. The characters between [] in a regular expression will match any of the characters in a string. So our above examples could be rewritten to /[f]/, which would still only match the letter f, and /[f][o][o]/, which will first try to match the set containing only a f, and after that an o and after that another o. But that is not what you want. You want to match (for example), the string ab. To that, we extend the set from containing one character to two characters : /[ab]/ will match (for example) afoo and bar and zob.

So you will want to construct from the answer data a regular expression and collect all correct answers in a []-set, and match the string against that RE.

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

Replies are listed 'Best First'.
Re: Re: Match function not providing results
by chriso (Sexton) on Dec 14, 2001 at 20:59 UTC
    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

      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