hi monks,
i am searching for a keyword in big file (536 mb). What i want as a result of this search is not only the keyword, but five words before and after the occurence of the keyword. What i do now is to input each line of the file into a string and split the string into individual words. Then i match the query word to each of these words from the file. Since they are in an array i extract five words before and after the keyword from the array and join them to make the result string . This approach works fine when i run this on a small file . But for size of file i am working with , this approach becomes very slow. I run this as a part of cgi script , so i get timeouts when the query word is actually towards the middle of file or much deeper. Can anybody tell me a better way to do it , or correct my approach , given the size of this file. I have not included the result string formation here.here is the code i use
an array @morgr is created here and shuffled to look for the word in r
+andom lines of the file
open (MORG, "< /home/sid/kwicionary/$filename") or die "Can't open $fi
+lename for reading: $!";
$path = "/home/sid/kwicionary/";
$indexname = "$path$filename.index";
sysopen(MIDX,$indexname, O_CREAT|O_RDONLY) or die "Can't open $indexna
+me for read/write:$!";
build_index(*MORG, *MIDX) if -z $indexname;
MORG:foreach (@morgr){
$line_number = $_;
$line = line_with_index(*MORG,*MIDX,$line_number);
if ($key =~ /\*/){
$key = substr($key,0,(length($key) -1));
@word = split(' ',$line);
foreach (@word){
if ($_ =~ /$key/){
push (@morg_results , $_);
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.