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

Im trying to write a little program that searches for a word entered by a user. The program should run through the files, and if it finds that word,it backs up, reads in some text, and prints it for the user. Basically what google does i guess. My problem is, that when the thing finds the match, it seems to go to the end of the file before i can start working on it. Here is is what im doing.

if(m/$key/i) { $wordMatchFilePosition = tell FILEHANDLE; seek FILEHANDLE, -1700, 1; $currentFilePosition = tell FILEHANDLE; ...

So when it finds that term the user searched for (named key here) it does this filehandling stuff which moves the position back. But, like i said, it seems to wait until the file is at the end to start doing this stuff, because im getting all the end portions of text printed out, instead of where the word was located.

Replies are listed 'Best First'.
Re: Files! Reading specific parts
by skyknight (Hermit) on Jul 29, 2003 at 18:41 UTC

    If you want to grab an arbitrary amount of text around something for which you are searching, just specify it in the regex. If you want the leading and trailing, say, 100 characters...

    /(.{0,100}$key.{0,100})/

    Afterwards, because of the parens, your match will be in the variable $1.

      Good idea, but i probably should have explained myself better. I need to filter out HTML as i read in the keyword. Something like this.... get keyword, go back some spaces, filter out the html and other keywords so its plain text, then go ahead of the word a bit and do the same.

        Then you should probably use some HTML parser class or other (HTML::Parser comes to mind), strip out all of the markup, and then use my previous suggestion. It should just be a matter of plugging together two modular solutions to solve a third problem that is really just two problems rolled into one.

Re: Files! Reading specific parts
by Not_a_Number (Prior) on Jul 29, 2003 at 19:11 UTC

    ++skyknight!

    Just an unrelated remark: if you're really searching for words rather than strings, don't forget to put word boundary markers in there:

    /(.{0,100}\b$key\b.{0,100})/

    Otherwise you could get some strange matches...

    dave

      Good point, though it would depend on the application. If you search on the word "house" and you're expecting that it will find both "house" and "houses", then you don't want the word boundary anchors, but if you're expecting to find the word exactly as you typed it, not as a substring of some pluralized word or whatnot, you are most certainly right. ++

        I speak from bitter experience, believe me. My day job is translating. Once upon a time, in a company that shall remain nameless where I was working, the helpful IT department came up, at my request, with an automated template translator into French (to avoid, in theory, our having to translate the exact same stuff every time we worked on a certain type of document: dates (January -> janvier), fields such as 'originated by', 'approved by', etc.):

        Approved by Paul Newby Approuvé par Paul Newpar

        Oh how we laughed.

        dave

Re: Files! Reading specific parts
by sgifford (Prior) on Jul 29, 2003 at 19:12 UTC
    Show us more of your program. How are you reading lines from the file?
      Well ill give you guys the whole function im using
      sub finder { @text = ""; $wordMatchFilePosition; $number = 0; $currentFilePosition; $lastPushed = space; $tagCount = 0; $quoteCount = 0; $loopcount = 0; if (!-d $File::Find::name) { open FILEHANDLE, "<$File::Find::name"; $size = -s $File::Find::name; $_ = ''; read FILEHANDLE, $_, $size; #on our first match, store the text behind and after the #word so the user can see a short desciption of why the #word appeared $file = $File::Find::name; if(!excludedDirectory()) { if(m/$key/i) { $wordMatchFilePosition = tell FILEHANDLE; seek FILEHANDLE, -1700, 1; $currentFilePosition = tell FILEHANDLE; for($loopcount = 0; $loopcount < 3000 && $currentFilePosition +!= $wordMatchFilePosition+700; $loopcount++) { $tempText = getc FILEHANDLE; #if we find the begining of the tag, increment the counter #dont add the text until we are back to 0 on the counter #(meaning were not in HTML commands) if($tempText eq "<") { $tagCount++; } #because were moving back an arbitrary number of character +s, #we might land in the middle of an HTML statement, therefo +re #if we hit a closing tag without having a begining tag #it should be safe to just ignore it elsif($tempText eq ">" && $tagCount > 0) { $tagCount--; } #More filtering of HTML elsif($tempText eq " \" ") { if($quoteCount == 1) { $quoteCount = 0; } else { $quoteCount = 1; } } #make sure were not reading HTML commands, the thing were +getting #is comprised of only letters, and were not trying to read + at the end #of the file, if its a space, and the last thing we pushed + on the text #was a letter, then its safe to assume that was the end of + a word #we are also filtering out numbers, because they are + used more with #defining pages then the actual content, we also dont want + things with _ #in it because its used for naming frames elsif($tagCount == 0 && $quoteCount == 0 && !eof FILEHAND +LE && $tempText =~ /(\b[A-Za-z]+\b)/ || $tempText =~ /^\s/ +&& $tempText ne "_") { if($lastPushed eq "character" && $tempText =~ /^\s/) { push(@text," "); $lastPushed = "space"; } else { push(@text,$tempText); $currentFilePosition++; $lastPushed = "character"; } } } print @text; print "\n"; } $number++ while/^${key}/g; close FILEHANDLE; $file = $File::Find::name; $length = length ($base_address) + 1; $file =~ /^.{${length}}(.*)/; if ($number) {$hash{$1} = $number} }#end if(!excludedDirectory()) } }
      Before i get into explaining, i looked at the HTML parser, and it was a bit more complicated then i was willing to invest time in. I just made my own, and it seems to catch most of the html. So what i do is call this subroutine and finds the match. Which works, im sure of it. Then i just run around that match taking off byte by byte and making sure that its not HTML. Then i just put it into an array, if i found a space insert it only if the last character i inserted with a letter. The result is something like this: joint venture The Chinese government certification process for the joint venture and the new company name should be completed later this summer its not perfect, but its "good enough" for what i want to do, and thats show people what they will see when they click on that link.

        Ah, OK. First you're getting the size of the file with the -s test. Then you're reading in the entire file with read---now the file position is at the very end of the file. Now, when you say "seek back 1700 characters from the current position", you're saying 1700 characters from the end of the file, since the file position is at the end of the file.

        I think you want to use the pos function, which will tell you where in the string the last match happened. Since you've read the entire file, the positions in the string will be equal to the positions in the file, so you can just seek to the position that pos returns.

        As others have suggested, though, you should consider using an HTML parser. HTML parsing is hard, and other people have already done it.