Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: assigning flat file dbs to arrays

by jarich (Curate)
on Apr 27, 2004 at 06:15 UTC ( [id://348406]=note: print w/replies, xml ) Need Help??


in reply to Re: assigning flat file dbs to arrays
in thread assigning flat file dbs to arrays

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.

Replies are listed 'Best First'.
Re: Re: Re: assigning flat file dbs to arrays
by Dente (Acolyte) on Apr 28, 2004 at 13:34 UTC
    Thank you for your response jarich. I tried your suggestion and now neither side would populate the flat file results are returned. However, I do feel more comfortable with the way you rewrote it because it is easier to handle. To truly understand my dilemma, I have included the following link. The HTML file before it is populated: http://www.urhosted.com/mall/searchres.html By doing a search for "sports" at the bottom of the HTML doc, you will see only the %%searcresults%% side is populated with the results from search.idx file. Example:
    20645 CJ Sports Sportswear and equipment retailer specialisi +ng in rugby, hockey, netball and cricket. Full product details and o +rdering available online. Apparel_and_Accessories-Athl +etic_Clothing www.cj-sports.co.uk/ 20660 Gekko Gear Selection of winter sports apparel. + Apparel_and_Accessories-Athletic_Clothing www.gekkogear.co +m/
    The data in the second file - search2.idx
    20225 URHosted Testing. Testing more. Apparel_and_Acces +sories-Athletic_Clothing www.urhosted.com
Re: Re: Re: assigning flat file dbs to arrays
by Dente (Acolyte) on Apr 28, 2004 at 21:13 UTC
    Thanks for code. I just needed to add a right bracket. However, the 2 arrays populate both includes showing the same search results. I there any way to adjust this? I like the way the arrays pull in the filehandlers so I'm hoping that does not have to be adjusted.
      To be honest, I'm not quite sure what you're asking here. So I'm going to take a wild stab in the dark. You say the 2 arrays populate both includes showing the same search results.. I'm not sure what you mean by both includes and I don't know if you've added any code to the section where I wrote do something with search results.

      If you have, you should show me the relevant code.

      What the code I've provided you with does is the following. For each element in @sidx, copy that element into $line and REMOVE an element from @sidx2 which is put into $premiumline. Note that this means that @sidx2 will be EMPTY at the end of the foreach loop. (To avoid this make a copy of @sidx2 before the foreach loop and unshift off that instead.)

      Once we've got $line and $premiumline we join them together into $sline. We then test if $sline matches what we're looking for in either $line or $premiumline and if it does we SAVE (only) $line into $resultline ($premiumline then gets thrown away).

      Maybe this isn't what you want to be doing. Maybe you want to be doing this:

      • Foreach element in @sidx, keep it (save it in @resultline) if it matches any test in @skeyw.
      • Foreach element in @sidx2, keep it (save it in @resultline2) if it matches any test in @premiumkeyw.

      Maybe you're trying to do something else...

      Since you haven't specified what you want to be doing I think it would be futile for me to guess. Think about what you want to do in a broader sense and I'll be able to help you better.

      All the best,

      jarich

        I am try to create a site similar to google. The main search results in one column and the other in another. Initially, I did not have this in mind at the onset on this project. Rather than do a complete rewrite, I was hoping the were alternative solutions to my dilemma. Foreach element in @sidx, keep it (save it in @resultline) if it matches any test in @skeyw. Foreach element in @sidx2, keep it (save it in @resultline2) if it matches any test in @premiumkeyw. Your code is the premise upon which I would like to build upon. Thank you for your time

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://348406]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found