in reply to Re: Files! Reading specific parts
in thread Files! Reading specific parts

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.

Replies are listed 'Best First'.
Re: Re: Re: Files! Reading specific parts
by sgifford (Prior) on Jul 30, 2003 at 21:48 UTC

    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.