I am trying to search a text file by reading the text file into my script, then tokenise the file, then search the token for $search. For reference there are 7 elements in the @database array.
sub search
{
# Load the flatfile into a data structure.
open(DB, $datafile);
{local $/; $_ = <DB>}
close(DB);
# Start printing template
print $templatehead;
# Get ready for interetting datafile
$recnum = 0;
RECORD: while (m/\G(.*?(?<!\\)(\\\\)*)\Q$dataseperator\E/gs)
{
# Get the next field and unescape it.
$data = $1;
$data =~ m/^\n?(.*?)\n?\\?$/s;
$data = $1;
$data =~ s@\\\\@\\@;
push @data, $1;
if ($#data >= $#database)
{
# Get what we are searching for.... the reason we are here.
$search = &getparam("search"); #gets a CGI::param()
foreach $field (@data)
{
if(-1 != index($field,$search))
{
&printfield(@data);
@data = ();
$recnum++;
next RECORD;
}
}
}
}
print $templatetail;
exit();
}
An example of the data in $datafile is,
Test|Subject|test@subject.com|Male|Perl-Database|good|on|
My|Tester|my@tester.com|Female|Perl-Database|very good|on|
Blank|Worker|blank@worker.com|Male|MySql-Database|the best!|off|
Someone|Somewhere|someone@somewhere.com|Female|Mysql-Database|great|of
+f|
Adding|Test|add@test.com|Male|Perl-Database|Great product!|on|
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|