?
Solved

writing and reading

Posted on 2000-01-26
4
Medium Priority
?
155 Views
Last Modified: 2010-03-05
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:
=======================================
#!/usr/bin/perl
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
"$FORM{'date'}|$FORM{'number'}|$FORM{'attendant1'}|$FORM{'attendant2'}|$FORM{'comment'}";

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

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



Here is the script I'm trying to write that searches the file:
======================================
#!/usr/bin/perl
$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'};

open(INF,$datafile);
@mydata = <INF>;
close(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) {
      chomp($i);
      ($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.

regards


0
Comment
Question by:fibdev
  • 2
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 2390648
What would be the difference between a script that prints all the data, and a search that matches everything?
0
 
LVL 3

Author Comment

by:fibdev
ID: 2390912
Ok,

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.  

:)
0
 

Accepted Solution

by:
glew012500 earned 400 total points
ID: 2392791
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
0
 
LVL 3

Author Comment

by:fibdev
ID: 2393546
glew,

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

ozo,

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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question