Hi everyone!
I'm new here and pretty much new to Perl, sorry if my question should sound lame but I really need your help..
I have a text file containing queries, one per row.
I put these query inside an array. What I want to do next is search a whole text file - sort of a dictionary (which has one entry per row and is formatted like this: word1]definition)for occurrences of the words contained in the array.
My code is the following:
open(HH, "<$PATHDATA/query.txt");
open(XX, "<$PATHDATA/dictionary.txt");
open(DD, ">$PATHDATA/query_results.txt");
my $query;
my $line="";
$i=0;
my @query;
while(<HH>){
chomp;
push @query,$_;
}
foreach $query (@query) {
while($line=<XX>){
if ($line =~ m/^($query)\].*/i) {
print DD "MATCH:$line\n";
if($debug){
print GREEN, "QUERY:$query\n";
print MAGENTA, "MATCH:$line\n";
}
}
}
}
I'd like to have something like this as an output:
QUERY:facebook
MATCH:facebook]social network....
The code I posted get me only the first result (because facebook is actually the first element in my array).
How can I get it to print every match for every possible query?
Thanks for your help,
Giu
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.