artisst has asked for the wisdom of the Perl Monks concerning the following question:

I have read and fully understand the humerous yet concisive "Clean Your Room" scenario. I appreciate you candor relating to my post.

I need to search 2 flat text files and return the search results in 2 separate columns. 1 column for international, and the other local listings. This search needs to open both files, read them, and return results. The HTML template I am using pulls search_line into a single column via %%searchresults%% . I have added a second include %%premiumlistings%% column adjacent to it. I know I need to assign each file to an array and then use the split / / function, but I seem to run into a problem using the while statement. I am using HTML::Template and Perl DBD::DBI Modules from CPAN. Tseek is a perl module inlcuded in the package but contains none of the variables necessary for search results. The data is text file which is tab delimited and the script is written totally in perl. Data files are created and stored in search.idx. I already have a solution to store user input data into search2.idx.

HTML Template:

<html> <head> <meta http-equiv="content-type" content="text/html;charset=iso +-8859-1"> <meta name="generator" content="Adobe GoLive 4"> <title>Search</title> <!-- Hide %%metadescription%% // End Hide --> </head> <body bgcolor="white"> <div align="left"> %%incl_mallheader%% <table border="0" cellspacing="0" cellpadding="1" width="8 +05" height="174"> <tr align="left"> <td width="23" valign="top"></td> <td valign="top" width="778"> <div align="left"> <table border="0" cellpadding="5" cellspac +ing="1" width="795"> <tr> <td width="556" valign="top"><font + face="Verdana" size="2"><strong>Search Results</strong></font><font +face="Verdana" size="2" color="#990000"><strong> </strong></font><fon +t face="Verdana" size="2">found </font><font face="Verdana" size="2" +color="#990000"><strong>%%link_matches%%</strong></font><font face="V +erdana" size="2"> matches searching for <strong>'</strong></font><fon +t face="Verdana" size="2" color="#990000"><strong>%%keywords%%</stron +g></font><font face="Verdana" size="2"><strong>'</strong></font><font + face="Verdana" size="2"><br> </font></td> <td valign="top" width="216" rowsp +an="4"><font face="Verdana" size="2">%%premiumlistings%%<br> </font> <p><font face="Verdana" size=" +2"><br> </font></td> </tr> <tr height="34"> <td width="556" height="34" valign +="top"><font face="Verdana" size="2"><br> %%searchresults%%<br> &nbsp;&nbsp;&nbsp;&nbsp; </fon +t></td> </tr> <tr> <td width="556" valign="top"> <center> <font face="Verdana" size= +"1">%%pages%%</font></center> </td> </tr> <tr> <td width="556" valign="top"></td> </tr> </table> </div> </td> </tr> </table> <table border="0" cellpadding="0" cellspacing="0" backgrou +nd="http://www.mdosn.com/mall/images/bar-bk.gif" width="741"> <tr height="29" bgcolor="#e5ecf9"> <td height="29" width="177"></td> <td height="29"> <form method="POST" action="http://www.mdosn.c +om/cgi-bin/directory/tsearch.cgi" enctype="x-www-form-urlencoded"> </td> <td height="29"></td> <td height="29"><input type="hidden" name="livebui +lt" value="live"></td> <td height="29" width="138"><input type="text" nam +e="keywords" size="21"></td> <td height="29" width="52"><font color="#003366" s +ize="1"><span class="lefttext"><input type="submit" value=" Search " +name="sbutton"></span></font></td> <td height="29"> </form> </td> <td height="29"><font color="#ff9900"> <form method="get" action="http://www.mdosn.co +m/cgi-bin/directory/malljump.cgi" onsubmit="location.href=jb.options[ +jb.selectedIndex].value; return false;"> </font></td> <td height="29"></td> cellspacing="0"> <tr> <td bgcolor="#3366cc" width="743"><img width="1" h +eight="1" src="(Empty Reference!)"></td> </tr> </table> <br> <table border="0" cellspacing="0" width="804" cellpadding= +"0"> <tr height="16"> <td width="804" valign="top" bgcolor="white" heigh +t="16"> <center> <font face="Verdana" size="1">%%incl_mallf +ooter%%</font></center> </td> </tr> </table> <p>&nbsp;</div> </body> </html>
Required is as follows
use Fcntl qw(:DEFAULT :flock); use CGI; use CGI::Carp qw(fatalsToBrowser); use tseek;
The script is as follow:
$main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g;
returns results in left column via %%searchresults%%
$main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%premiumlistings%%/$premiumitem/g;
unfortunatly returns same results in right column via %%premiumresults%%. This is where the problem starts:
sub normal_search { ### PERFORM SEARCH $search_line = $fields{'keywords'}; $icnt = 0; $toadk = ""; (@skeyw) = split(/ /,$search_line); $nrkeywords = 0; foreach $item (@skeyw){$nrkeywords++;} open (SIDX, "$data_dir/search.idx"); added code below to open 2nd file open (SIDX2, "$data_dir/search2.idx"); if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set lock for file: $data_dir/search.idx, $data_dir/search2.idx $!\n";}
here's 1st problem (only opening and reading 1st file
while (defined($line=<SIDX>)) { $sline = $line; foreach $kwr (@skeyw) { if (($sline =~ /$kwr/i) and ($kwr ne "")) { $toadk = "true"; } } if ($toadk eq "true") { $resultline[$icnt] = $line; $toadk = false; $icnt++; } } #if ($file_locking ne "No"){flock (CIT, LOCK_UN);} close (SIDX); }
I need this to open both files and return the results of search.idx in the right column and search2.idx in the right. This is the latter area of importance:
$resultline[$rcc] = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\t" . $row[3] . "\t" . $row[4] . "\t" . $row[5] . "\t" . $row[6] . "\t" . $row[7] . "\t" . $row[8] . "\t" . $kinclude . "\n"; $rcc++; } $sth->finish; $dbh->disconnect; }
and finally:
sub get_search_ready { my ($search_line) = @_; $reline = $search_line; $reline =~ s/\+//g; $reline =~ s/\[//g; $reline =~ s/\]//g; $reline =~ s/\)//g; $reline =~ s/\(//g; $reline =~ s/\*//g; $reline =~ s/\^//g; #$reline =~ s/\.//g; $reline =~ s/\$//g; $reline =~ s/\?//g; $reline =~ s/\\//g; $reline =~ s/\~//g; $reline =~ s/<//g; $reline =~ s/>//g; $reline =~ s/;//g; return ($reline); }

janitored by ybiC: Balanced <readmore> tags around longish codeblock

Replies are listed 'Best First'.
Re: Separating arrays for column search results
by jeffa (Bishop) on Apr 24, 2004 at 14:23 UTC

    Well, what does your data look like? If you give us the data and the requirements, we can tell you how to write the code. But if you just post code, all we can really do is critique it.

    You mentioned an HTML template ... are you using HTML::Template or Template Toolki? Or are you rolling your own? You say you are searching 2 flat files. How is the data delimited? By spaces? Have you ever heard of DBD::CSV or DBD::AnyDATA? They can ease searching. I prefer to store data in a Relation Database like MySQL or Postgres.

    This snippet of code:

    $resultline[$rcc] = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\t" . $row[3] . "\t" . $row[4] . "\t" . $row[5] . "\t" . $row[6] . "\t" . $row[7] . "\t" . $row[8] . "\t" . $kinclude . "\n";
    Can be rewritten consisely and maintainably as:
    $resultline[$rcc] = join("\t", @row[0..8]) . "$kinclude\n";

    We would love to help you more, but you really should Clean your room first.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Separating arrays for column search results
by Stevie-O (Friar) on Apr 24, 2004 at 15:27 UTC
    This code:
    $reline =~ s/\+//g; $reline =~ s/\[//g; $reline =~ s/\]//g; $reline =~ s/\)//g; $reline =~ s/\(//g; $reline =~ s/\*//g; $reline =~ s/\^//g; #$reline =~ s/\.//g; $reline =~ s/\$//g; $reline =~ s/\?//g; $reline =~ s/\\//g; $reline =~ s/\~//g; $reline =~ s/<//g; $reline =~ s/>//g; $reline =~ s/;//g;
    can also be condensed:
    $reline =~ tr/+[]()<>*^$?\\~;//d;
    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
Re: Separating arrays for column search results
by dragonchild (Archbishop) on Apr 25, 2004 at 19:12 UTC
    Instead of reposting your question in its entirety (did you just cut'n'paste and rename it, hoping it wouldn't be noticed?), try and rephrase your question. Break it down to the smallest possible reprodicible test case. Often, I have found that by creating the smallest possible example, I have solved my problem.

    Your first problem is that you're not using a standard templating module, like HTML::Template or Template Toolkit. Use one of those - they have solved the issue I see you asking about. Plus, we'll actually be able to understand your question and give you meaningful answers.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose