put chosen words into file

id like a script that loads up a text file "myfile.txt" and places a checkbox after each word. When a checkbox is selected that number word and word are placed into a file

ie
if "myfile.txt" contains
here is my story. thanks for reading.
when i execute my script i will see
here [] is [] my [] story []. thanks [] for [] reading [].

there will be a submit and reset button at the end and if for example i click the check boxes after "here" "thanks" and "reading" and file called "gaps.txt" will then contain
1 - here
5 - thanks
7 - reading

thank u
boofullsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MindoConnect With a Mentor Commented:
Here's is the first script that generates the checkboxes on the form:

========================================
=== file.cgi
========================================
#!/usr/local/bin/perl -w

use CGI qw(:standard);

$q = new CGI;

print $q->header,
      $q->start_html("Form Demo"),
      $q->h1("Form Demo");

$method = "POST";
$value = "";
$action = "http://www.soften.ktu.lt/~genumind/perl/fileres.cgi";

print $q->startform($method, $action, "");
print "<br>";

open(F, "< myfile.txt");
$count = 0;
while(<F>)
{
  for $word (split)
  {
    $count++;
    print $q->checkbox_group(-name=>"n".$count,
                             -values=>[$word]);
  }
}

close(F);

print "<br>";
print $q->submit;
print $q->reset;
print "<br>";

print $q->endform;
print $q->end_html;

open(F, "> cnt.txt");
print F $count;
close(F);
========================================

Here's a sample words file:
========================================
=== myfile.txt
========================================
one two three
four five six
seven eight nine
========================================

Here's the second script which receives results from the first one and prints the results to the resulting file gaps.txt:

========================================
=== fileres.txt
========================================
#!/usr/local/bin/perl -w

use CGI qw(:standard);

$q = new CGI;

open(F, "< cnt.txt");

$count = <F>;

close(F);

open(F, "> gaps.txt");

for($i = 1; $i <= $count; $i++)
{
  $value = param("n".$i);
  if(length($value) > 0)
  {
    print F "$i - $value\n";
  }
}

close(F);

print $q->header,
      $q->start_html("Form Results Done"),
      $q->h1("Form Results Done");

print "<br>";

print $q->endform;
print $q->end_html;

========================================

========================================
=== gaps.txt
========================================
1 - one
4 - four
5 - five
7 - seven
========================================

I tested this example on the following address:

http://www.soften.ktu.lt/~genumind/perl/file.cgi

So i wrote the core - now do some programming and adjust it to your needs.

Cheers.
0
 
boofullsAuthor Commented:
can u tell me what the cnt.txt file is for?
thanks
0
 
MindoCommented:
cnt.txt file is to save the number of words in the myfile.txt.
0
 
boofullsAuthor Commented:
ok thats great
one kinda related questionso i put the points up

in different code the story file is loaded up again and drop down menus are placed in....but it prints the story without periods "." and "?" "!" etc

i think the problem is here

   while(<F>)
   {
   while(/(\w+)/g)
   {
etc.......

should i change the second while loop so it will load up "!" "." "?" etc.....

thanks
0
 
MindoCommented:
The \w+ means that only the words are taken. What is meant by \w you can take a look at Perl manual. As i remember it's lowercase and uppercase letters, the undescore.

Do the following:

while(<F>)
   {
   while(/([\w!.?]+)/g)
   {
etc.......

0
All Courses

From novice to tech pro — start learning today.