I have come to seek wisdom from those who know. Where others have failed, I hope you can provide (a solution of course). Below is a quiz script to produce questions and answer boxes. Following that script is a script I've created which will eventually grade the quizes. This is the script where the problems lie. I'm having two problems.
One is with the match operator not providing a proper match in the 'if' statement. If I set up the if statement to match more than one character (ie. =~ /ad/i), it doesn't work. If I match only one character, it works fine (ie. =~ /d/i).
To see what the problem is, I then print out s1-s4 to make sure the characters I am trying to match are really there. I also decided to print out the key/value pairs in three different ways, each separated by the horizontal rule. Herein lies the second problem.
Note that the first two ways print out the key and values correctly. However, the third method prints the key correctly, but only prints out the first character of the value. Strange. I have tried the script using perl 5.6 for NetWare, and also ActiveState perl 5.6 and get the same results. Can anyone help me???? I've also included the text file being used to input the various data. Many thanks.

******** CODE TO PRINT QUIZ

use CGI qw(:standard); print header; local $/ = "~\n"; $infile = param('file'); print start_html ( -title=>'quiz', -BGCOLOR=>'beige' ); print start_form ( -method=>'POST', -action=>"http://netlab/perl/obenberger/gradequiz.cgi" ); open (FILE, $infile) || die "Cannot open $infile: $!"; while (<FILE>) { chomp; ($type, $value) = split (/:/, $_); if ($type =~ m/q/i) { # ******* CHECKS FOR QUESTION LINE print "<B>$value</B><BR>"; } if ($type =~ m/a/i) { # ********* CHECKS FOR ANSWER LINE @answers = split (/,/, $value); $anslength = @answers; } if ($type =~ m/^s/) { # *** DETERMINES TYPE OF ANSWER BOX %selections = split (/,/, $value); @options = keys (%selections); if ($anslength > 1) { print checkbox_group(-name=>$type, -values=>\@options, -linebreak= +>'true', -labels=>\%selections); }else { print "<BR>"; print radio_group(-name=>$type, -values=>\@options, -linebreak=>'t +rue', -labels=>\%selections, -default=>'-'); } print "<BR><HR><BR>"; } } # ********** END WHILE STATEMENT close (FILE); print "<CENTER>"; print submit (-value=>'Grade') . "&nbsp; &nbsp;"; print reset (-value=>'Reset answers'); print "</CENTER>"; print end_form; print end_html;
*** CODE TO GRADE QUIZ - THIS IS WHERE THE PROBLE IS.
use CGI qw(:standard); print header; print start_html (-title=>'graded quiz', -BGCOLOR=>'beige'); # The match doesn't work if more than one character is matched if(param('s1') =~ m/ad/i) { print "<B><BR>Correct! Congradulations! Your answer was a</B><P>"; }else{ print "<B><BR>Incorrect! You did not mark the correct answer!</B><P>"; } print "<BR>The value for s1 is: <B>"; print param('s1'); print "</B><P>"; print "The value for s2 is: <B>"; print param('s2'); print "</B><P>"; print "The value for s3 is: <B>"; print param('s3'); print "</B><P>"; print "The value for s4 is: <B>"; print param('s4'); print "</B><P>"; print "<HR>"; foreach $key (param()) { print "NAME = "; print $key; print " and VALUE = "; print param($key); print "<P>"; } print "<HR>"; # Only the first character of the value is printed. Why? foreach $key (param()) { print "NAME = " . $key . "and VALUE = " . param($key) . "<P>"; } print end_html;
********* DATA
q1:Which are two items found in the kitchen?~
a1:a,d~
s1:a,pot,b,wrench,c,screw driver,d,fork~
q2:How much is 2 + 2?~
a2:b~
s2:a,2,b,4,c,5,d,3,e,1~
q3:Which of the following are modes of transportation?~
a3:a,c~
s3:a,car,b,fence,c,plane,d,pants~
q4:The part of the body that contains the brain is the ______~
a4:c~
s4:a,arm,b,stomach,c,head,d,leg,e,foot

In reply to Match function not providing results by chriso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.