writing and reading

I'm trying to create three cgi scripts to write, search, and dump all the data stored in a text database respectivly.  I've got the write script working.  As for the search script, It works sortof, but it won't post more than one result per query.

Here is the script that posts the data:
print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $value =~ s/\n/ /g;# added to strip line breaks

    $FORM{$name} = $value;

open(OUTF,">>celibclass.db") or dienice("Couldn't open
celibclass.db for writing: $!");

print OUTF

print <<EndHTML;
<html><head><title>Thank You</title>
<link rel="stylesheet" href="http://fibdev.com/syles/url.css">
<h2>Thank You!</h2>
Thank you for keeping me updated.<p>
<a href="http://fibdev.com/index.htm">home</a><p>

sub dienice {
    my($msg) = @_;
    print "<h2>Error</h2>\n";
    print $msg;

Here is the script I'm trying to write that searches the file:
$datafile = "celibclass.db";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/~!/ ~!/g;
    $FORM{$name} = $value;

$searchstr = $FORM{'date'};

@mydata = <INF>;
print "Content-type:text/html\n\n";
print "<html><head><title>Search Results</title></head>\n";
print "<body><h3>Search Results</h3>\n";

@results = grep(/$searchstr/i,@mydata);
if ($#results >= 0) {
    foreach $i (@results) {
      ($date, $number, $attendant1, $attendant2, $comment) = split(/\|/,$i);
      print "<b>$date</b> - $attendant1, $attendant2<br>\n";
        print "Comment:\n";
        print "$comment<br><br>\n";
} else {
    print "No results found.<p>\n";
print </body></html>\n";

As for the script that neatly posts all the data in the file, I don't know enough to go there, yet anyway.  perl is new to me.

Any help with this write and search will be helpful. If someone wants to write me a script that neatly posts all the data, I'll make it worth your while in points.  I've got pleanty of points to spend.


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.

What would be the difference between a script that prints all the data, and a search that matches everything?
fibdevAuthor Commented:

As I said, perl is new to me.  What I want to accomplish has been laid out.  If there is a better way than to use three different scripts I welcome it, but I don't know enough about perl to impliment it.  

as far as i can see, grep only returns one result because you're only writing one (admittedly long) line into the .db file:
reading a filehandle into an array will split by newlines by default, so there's only one entry in @mydata, whgich grep returns, ans the split dumps the rest on the entries.

in the following:

print OUTF                    "$FORM{'date'}|$FORM{'number'}|$FORM{'attendant1'}|$FORM{'attendant2'}|$FORM{'comment'}";

try adding a \n to the end, so you'll get by with the default splits.
you might also use any other record seperator, but then you'll have to do a bit on adjusting in the search script.

as for outputting all data: ozo's comment is a good idea. you won't need an extra script, but just run the search-script with an empty ("") search string passed, so grep will return all records.

hope it helps, glew

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
fibdevAuthor Commented:

Thank you for pointing out that one little thing that fixed the whole works!


Thank you for your suggestion, even though it was glew that got the answer for me, I'm giving you some points as well.  See the perl topic area for your points.

I'm submitting a new question about my search.cgi script that I would like you guys to look at.

Thanks again
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

From novice to tech pro — start learning today.