guess words

say i have a text file "answers.txt" like this:
1 - test
3 - file
5 - now
10 - the

i would like a script that will load up a text file "story.txt" a do this:

replace all words in the relevant positions with a drop down menu containing the words in answers.txt

for example
if story.txt is:
this is a test file that i will test out now.

then when i run my script it get
[test/file/now/the] is [test/file/now/the] test [test/file/now/the] that i will test
[test/file/now/the] now.

note words 1 3 5 and 10 have been replaced with a drop down menu with all the words in answers.txt

then at the bottom of this script there is a submit button and when users hit submit they are told how many they got correct/ incorrect

thanks

paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

windfallCommented:
I am fairly new to perl but you may want to try something like this:
It uses two cgi scripts----
(I'm sure one of the real pro's has a much more elegant answer)

******************************************
#!/usr/bin/perl
##########################################################################
# PROGRAM    answertest.pl
use CGI qw/escape unescape/;
%answers = ( #can construct this list by reading in text file
             # or use DBI and a database etc
           1 => "test",
           3 => "file",
           5 => "now",
           10=> "the"      
      );
@answerkey = keys(%answers);#make an array for sorting
@sortedans = sort { $a <=> $b } @answerkey;#keeps in order

$answer_Sel = "";#make select options for drop down box                              
foreach $i (@answerkey) {
$answer_Sel .= "<OPTION Value = $i>$answers{$i}";
            }

#could be read in from a text file
$story = "this is a test file that i will test out now.";
#make an array of the story split on spaces
@story = split(/ /, $story);
#make an array of positions to put form elements
@position = ('1','3','5','10');
foreach $i(@position){
splice(@story,$i,1,("<select NAME=Q1>$answer_Sel</select>"));
      }         
$query = new CGI;
#send MIME and HTML headers
print $query->header("-nph=>1");
print $query->start_html(-title => 'Test answers!');
print <<HTML;
<FORM NAME="whatever" ACTION=/cgi-bin/whateveranswer.pl METHOD=Post ENCTYPE=multipart/form-data>
HTML
print "@story";
print <<HTML;
<input TYPE="submit" VALUE="How many correct?">
</FORM>
HTML
print $query->end_html;


**************************************************

#!/usr/bin/perl
##########################################################################
# PROGRAM    whateveranswer.pl
use CGI qw/escape unescape/;

$query = new CGI;
@question=$query->param("Q1");

$correct = "0";
#make an array of correct answers
@answerkey = ("3", "5","10","1");

foreach $i(@question){
            if ($i = $answerkey[$i]){
            $correct++
            }
            }
            
            
$query = new CGI;
#send MIME and HTML headers
print $query->header("-nph=>1");
print $query->start_html(-title => 'Test correct!');
print <<HTML;

You got $correct correct!

HTML
print $query->end_html;
0
paulwhelanAuthor Commented:
th eexample i gave was just hypothetical
id like it to open a file and for it to work on any file......

thanks for the help though
0
funksoulCommented:
This is very rough...just to give you an idea!


#!/usr/local/bin/perl5

use CGI qw(:standard);

&parse_story;
&parse_ans;
for ($i=0; $i<scalar(@num_arry); $i++){
  $data[$num_arry[$i]]= "popupmenu";
}

$query= new CGI;
$method= "text.cgi";  #name of your program
$action= "POST";
$encoding= "application/x-www-form-urlencoded";
print header(), start_html("TITLE");
print $query->startform($method,$action,$encoding);

for ($i=0; $i<scalar(@data); $i++){
  if ($data[$i] eq "popupmenu"){
     print $query->popup_menu(-name=>'menu_name',
                              -values=> [@word_arry],
                              -default=> '?????');
  }
  else{
     print " $data[$i]";
  }
}
 

print $query->submit(-name=>'Submit');
print $query->endform;
print end_html;

sub parse_story{
  open STORY, "story.txt";
  $line= "";
  while (<STORY>){
    chomp; #remove carriage
    $line= "$line $_";         # Concatenate the lines
  }
  @data= split /\s+/, $line;
  print @data;
  close STORY;
}

sub parse_ans{
  open ANSWERS, "answers.txt";
  $i=0;
  while (<ANSWERS>){
    chomp; #remove carriage
    ($num, $word)= split /\s-\s/, $_, 2; # Get the number and word
    $num_arry[$i]= $num;
    $word_arry[$i]= $word;
    $i++;
  }
  close ANSWERS;
}

 
0
funksoulCommented:
funksoul changed their proposed answer to a comment
0
funksoulCommented:
This is very rough...just to give you an idea!

#!/usr/local/bin/perl5

use CGI qw(:standard);

&parse_story;
&parse_ans;
for ($i=0; $i<scalar(@num_arry); $i++){
  $data[$num_arry[$i]]= "popupmenu";
# replace the word with "popupmenu" all the answer positions.
}

$query= new CGI;
$method= "text.cgi";  #name of your program
$action= "POST";
$encoding= "application/x-www-form-urlencoded";
print header(), start_html("TITLE");
print $query->startform($method,$action,$encoding);

for ($i=0; $i<scalar(@data); $i++){
  if ($data[$i] eq "popupmenu"){  
#print the selection if the word is "popupmenu"
     print $query->popup_menu(-name=>'menu_name',
                              -values=> [@word_arry],
                              -default=> '?????');
  }
  else{
     print " $data[$i]";
  }
}
 

print $query->submit(-name=>'Submit');
print $query->endform;
print end_html;

sub parse_story{
  open STORY, "story.txt";
  $line= "";
  while (<STORY>){
    chomp; #remove carriage
    $line= "$line $_";         # Concatenate the lines
  }
  @data= split /\s+/, $line;
  close STORY;
}

sub parse_ans{
  open ANSWERS, "answers.txt";
  $i=0;
  while (<ANSWERS>){
    chomp; #remove carriage
    ($num, $word)= split /\s-\s/, $_, 2; # Get the number and word
    $num_arry[$i]= $num;
    $word_arry[$i]= $word;
    $i++;
  }
  close ANSWERS;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.