Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Array extraction and separate returns

by Dente (Acolyte)
on May 25, 2004 at 15:11 UTC ( [id://356245]=perlquestion: print w/replies, xml ) Need Help??

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

I have been successful n reading from 2 files and storing contents in an array but now I need to be able to extract the keywords from the array - @skeyw - that has the combined contents of 2 files <SIDX> and <SIDX2>, and split them into 2 separate returns, one return from the keywords found in <SIDX> and the other keywords found in <SIDX2>. They will be inserted into the html placeholders %%searchresults%% and the other in %%premiumresults%%:
$main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g; $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%premiumresults%%/$pitem/g; print "Content-type: text/html\n\n"; $main_template = tseek::insert_tmpl_includes($main_template); print "$main_template"; exit;
Here is where I do the search:
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"); 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($line3 = <SIDX>) { $line2 = <SIDX2>; $sline = "${line2}${line3}"; foreach $kwr (@skeyw) { if (($sline =~ /$kwr/i) and ($kwr ne "")) { $toadk = "true"; } } if ($toadk eq "true") { $resultline[$icnt] = "${line2}"; $premium_resultline[$icnt] = "${line3}"; $toadk = false; $icnt++; } } #if ($file_locking ne "No"){flock (CIT, LOCK_UN);} close (SIDX); close (SIDX2); }
and here is where I need to split the keyword search results from both files that are still contained in @skeyw. Search results from <SIDX> assigned to ($search_line) = @_; and the search results from <SIDX2> assigned to ($premium_search_line) = @_;
sub get_search_ready { my ($search_line) = @_; $reline = $search_line; $reline =~ s/[+\[\]()*^.\$?\\~<>;]//g; return ($reline); } sub get_search_ready2 { my ($premium_search_line) = ${line2}; $premium_reline = $premium_search_line; $premium_reline =~ s/[+\[\]()*^.\$?\\~<>;]//g; return ($premium_reline); }

Replies are listed 'Best First'.
Re: Array extraction and separate returns
by xorl (Deacon) on May 25, 2004 at 15:39 UTC
    Why can't you use two arrays, one for <SIDX> and one for <SIDX2>? If the results from the two files are going to two different places why not keep them seperate to start with? Yes this means You'll have to do two searches, one on each array. The only other idea I have is when you join the two lines into the array, have some known character divider (i.e. ":") and just split the result on that character.
      Upon further examination, I have concluded that what i really need is an loop statement stating that if the keywords are found in <SIDX> then this:
      $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g;
      and if the keywords are found in <SIDX2> then this:
      $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%premiumresults%%/$pitem/g; print "Content-type: text/html\n\n"; $main_template = tseek::insert_tmpl_includes($main_template); print "$main_template"; exit;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://356245]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-23 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found