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

I need to search 2 flat text files and return the search results from each file individually in 2 separate columns. The HTML template I am using pulls search_line into a column via %%searchresults%%. I know I need to split the array nd assign the 2nd file a handle. Beyond this I am lost. I have added a second include %%premiumlistings%% column adjacent to it. Sample search.idx Source (search2.idx is also tab delimited:
20660 Gekko Gear Selection of winter sports apparel Apparel_a +nd_Accessories-Athletic_Clothing www.gekkogear.com/
HTML Template Includes: Simple HTML doc created in GoLive %%searchresults%% %%premiumlistings%% Required is as follows
use Fcntl qw(:DEFAULT :flock); use CGI; use CGI::Carp qw(fatalsToBrowser);
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;

janitored by ybiC: Closed final <code> tag, replaced (excessive for viewing) tabs with spaces, balanced <readmore> tags around longish codeblock

Replies are listed 'Best First'.
Re: assigning flat file dbs to arrays
by Zaxo (Archbishop) on Apr 25, 2004 at 22:00 UTC

    I think you want to split on tab instead of space, (@skeyw) = split(/\t/,$search_line);

    After Compline,
    Zaxo

      I need something similar to node_id=266457. The idea is to grab search results out of 2 tab delimited text files but keep the results of each file in separate scalers or maybe arrays to be printed separately later.
      open (SIDX, "$data_dir/search.idx"); open (SIDX2, "$data_dir/search2.idx"); if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set lo +ck for file: $data_dir/search.idx, $data_dir/search2.idx $!\n";} while (defined($line=<SIDX>) and ($premiumline=<SIDX2>)) { $sline = $line, $premiumline; foreach $kwr (@skeyw, @premiumkeyw) { if (($sline =~ /$kwr/i) and ($kwr ne "")) { $toadk = "true"; } } if ($toadk eq "true") {
      Here is where I need the premiumline search results from SIDX2 along with the resultline form SIDX.
      $resultline[$icnt] = $line; $toadk = false; $icnt++; } } #if ($file_locking ne "No"){flock (CIT, LOCK_UN);} close (SIDX); close (SIDX2); }
        I've looked and looked at your code, read the parent node and your other one and I'm not quite sure what you're actually trying to do. Still I think that you would be better off replacing the above code with the following:
        # Attempt to lock only $data_dir/search.idx, if allowed if ($file_locking ne "No") { flock (SIDX, LOCK_SH) or die "Can't set lock for ". "file: $data_dir/search.idx: $!"; } # Read whole files in to arrays. This is a really bad # thing to do if the files are potentially huge. If # they are small (< 1000 lines), however this is okay. my @sidx = <SIDX>; my @sidx2 = <SIDX2>; # Closing filehandles removes the locks close (SIDX); close (SIDX2); # Make sure that the search files have the same lengths (I'm # guessing here, perhaps this isn't important to you). unless(@sidx = @sidx2) { die "Search files are of differing lengths."; } # Remove empty strings from @skeyw and @premiumkeyw @skeyw = grep {$_ ne ""} @skeyw; @premiumkeyw = grep {$_ ne ""} @premiumkeyw; # Build a big regular expression out of @skeyw and # @premiumkeyw for faster matching. my $regexp = join('|', @skeyw, @premiumkeyw); $regexp = qr/$regexp/; foreach my $line (@sidx) { my $premiumline = unshift(@sidx2); # destructive. # In the previous code you write: # $sline = $line, $premiumline; # this is the same as: # $sline = $line; # I'm going to guess that you mean the following: $sline = "$line, $premiumline"; # Do our test to see if this matches our regexp if($sline =~ /$regexp/) { # do something with search results $resultline[$icnt] = $line; $icnt++; } }
        That should provide the same results as your code but faster and maybe more flexibly.

        Hope this helps

        jarich

        Update: Fixed rather embarrassingly wrong use of != instead of ne in grep.

        Also changed pop to unshift so elements come through in the same order.