O
Oeln
I've got a flat text file db (tab delimited) & a form that lets one
query it online. In its current incarnation, it only lets one input a
term to look for in the file, & outputs the lines in the file which
include that term. If the input 'term' incldes ' ', it considers it
one term. I'd like to increase its functionality by including the &/or
option (&, of course, therefore identifying each term input to the
form individually).
The form that includes the query option:
<form method="post" action="cgi-bin/odb.pl">
<input type="text" name="input">
<input type="hidden" name="oper" value="query">
<input type="submit" value="Okay">
<input type="reset" value="Clear">
</form>
In odb.pl I've got the following in order to parse the form itself:
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
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;
$contents{$name} = $value;
}
}
I've then got, in order to do the query, the following:
if ($contents{'oper'} eq "query") {
open (FILE, "$odb") || do {&no_open;};
&output_header;
&output_open;
$count=0;
@ordered = sort(<FILE>);
foreach $pair (@ordered) {
if ($pair =~ /$contents{'input'}/gi) {
$count++;
@item = split(/\t/, $pair);
&output_item;
}
}
if ($count==0) {
# print " ";
}
close(FILE);
&output_close;
&output_footer;
exit;
}
It works fine like this: I've got no issues with it other than it's
lack of the &/or option. In order to offer this, I'd obviously include
in the form itself an 'option' field, in order to let one indicate
&/or: i.e.,
<form method="post" action="cgi-bin/odb.pl">
<input type="text" name="input">
<input type="radio" name="option" value="and">&
<input type="radio" name="option" value="or">or
<input type="hidden" name="oper" value="query">
<input type="submit" value="Okay">
<input type="reset" value="Clear">
</form>
I've gone over the original code I've got, & changed it to be the
following instead:
if ($contents{'oper'} eq "query") {
@inputs = split(/\s+/, $contents{'input'});
# $odb file location is identified elsewhere
open(FILE,"$odb") || do {&no_open;};
@ordered = sort(<FILE>);
foreach $pair (@ordered) {
# &
if ($contents{'option'} eq "and") {
foreach $input (@inputs) {
if (!($pair =~ /$input/i)) {
$include{$pair} = 'no';
last;
} else {
$include{$pair} = 'yes';
}
}
# or
} elsif ($contents{'option'} eq "or") {
foreach $input (@inputs) {
if ($pair =~ /$input/i) {
$include{$pair} = 'yes';
last;
} else {
$include{$pair} = 'no';
}
}
}
}
&output_header;
&output_open;
foreach $key (keys %include) {
if ($include{$key} eq 'yes') {
$count++;
@item = split(/\t/, $pair);
&output_item;
}
}
if ($count == 0) {
# print " ";
}
&output_close;
&output_footer;
close(FILE);
exit;
}
I'd imagine this would identify the individual terms in the 'input'
field, & then, for each line in the file it would look for an
occurence of one of the input terms. If the option indicated were
'and', it would look for an instance of a term not incl. in the line &
if it found one, it would not include that line in output (the
opposite would then occur for 'or') in order for it to be a little bit
faster than looking for each term. I guess for now though I'm less
interested in it being fast than in getting it to work.
The interesting thing is this: I've got the following it &output_item:
sub output_item {
print <<"OUTPUT";
<tr>
<td>$item[0]</td>
<td>$item[1]</td>
<td>$item[2]</td>
</tr>
OUTPUT
}
In the output I'm getting now, there is an increase in table cells if
I've input a term that ought to be found in the text file. If I input
a term that it won't find, it gives me no table cells. I'm getting
this impression by looking at the table border outline cells in the
output - it's like it's finding the items in the file & outputting
empty table cells like I've indicated if it finds a term; but it's not
including the text in $item[0], $item[1], etc. I've got no idea why,
or where the issue is, if I'm overlooking the obviuos or not..
- Noel
query it online. In its current incarnation, it only lets one input a
term to look for in the file, & outputs the lines in the file which
include that term. If the input 'term' incldes ' ', it considers it
one term. I'd like to increase its functionality by including the &/or
option (&, of course, therefore identifying each term input to the
form individually).
The form that includes the query option:
<form method="post" action="cgi-bin/odb.pl">
<input type="text" name="input">
<input type="hidden" name="oper" value="query">
<input type="submit" value="Okay">
<input type="reset" value="Clear">
</form>
In odb.pl I've got the following in order to parse the form itself:
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
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;
$contents{$name} = $value;
}
}
I've then got, in order to do the query, the following:
if ($contents{'oper'} eq "query") {
open (FILE, "$odb") || do {&no_open;};
&output_header;
&output_open;
$count=0;
@ordered = sort(<FILE>);
foreach $pair (@ordered) {
if ($pair =~ /$contents{'input'}/gi) {
$count++;
@item = split(/\t/, $pair);
&output_item;
}
}
if ($count==0) {
# print " ";
}
close(FILE);
&output_close;
&output_footer;
exit;
}
It works fine like this: I've got no issues with it other than it's
lack of the &/or option. In order to offer this, I'd obviously include
in the form itself an 'option' field, in order to let one indicate
&/or: i.e.,
<form method="post" action="cgi-bin/odb.pl">
<input type="text" name="input">
<input type="radio" name="option" value="and">&
<input type="radio" name="option" value="or">or
<input type="hidden" name="oper" value="query">
<input type="submit" value="Okay">
<input type="reset" value="Clear">
</form>
I've gone over the original code I've got, & changed it to be the
following instead:
if ($contents{'oper'} eq "query") {
@inputs = split(/\s+/, $contents{'input'});
# $odb file location is identified elsewhere
open(FILE,"$odb") || do {&no_open;};
@ordered = sort(<FILE>);
foreach $pair (@ordered) {
# &
if ($contents{'option'} eq "and") {
foreach $input (@inputs) {
if (!($pair =~ /$input/i)) {
$include{$pair} = 'no';
last;
} else {
$include{$pair} = 'yes';
}
}
# or
} elsif ($contents{'option'} eq "or") {
foreach $input (@inputs) {
if ($pair =~ /$input/i) {
$include{$pair} = 'yes';
last;
} else {
$include{$pair} = 'no';
}
}
}
}
&output_header;
&output_open;
foreach $key (keys %include) {
if ($include{$key} eq 'yes') {
$count++;
@item = split(/\t/, $pair);
&output_item;
}
}
if ($count == 0) {
# print " ";
}
&output_close;
&output_footer;
close(FILE);
exit;
}
I'd imagine this would identify the individual terms in the 'input'
field, & then, for each line in the file it would look for an
occurence of one of the input terms. If the option indicated were
'and', it would look for an instance of a term not incl. in the line &
if it found one, it would not include that line in output (the
opposite would then occur for 'or') in order for it to be a little bit
faster than looking for each term. I guess for now though I'm less
interested in it being fast than in getting it to work.
The interesting thing is this: I've got the following it &output_item:
sub output_item {
print <<"OUTPUT";
<tr>
<td>$item[0]</td>
<td>$item[1]</td>
<td>$item[2]</td>
</tr>
OUTPUT
}
In the output I'm getting now, there is an increase in table cells if
I've input a term that ought to be found in the text file. If I input
a term that it won't find, it gives me no table cells. I'm getting
this impression by looking at the table border outline cells in the
output - it's like it's finding the items in the file & outputting
empty table cells like I've indicated if it finds a term; but it's not
including the text in $item[0], $item[1], etc. I've got no idea why,
or where the issue is, if I'm overlooking the obviuos or not..
- Noel