Update: OP (promptly!) fixed the formatting.
++ rakesh01.
Retained my comments, from "---" to "------" only for future newcomers

---

Your "very important and urgent" (twice, yet!) would come a whole lot closer to being my "important and urgent" had you taken time to format your post properly. The input page provides the minimal markup needed here; the preview function should have told you that you were making a mess.

Please fix it. The Monks are most generous with their assistance... when they read an appropriate question -- and yours appears to be appropriate (though it skirts perilously close to "fix it for me") -- with minimal difficulty (which is NOT the case above).

Please see Writeup Formatting Tips, Markup in the Monastery, Perl Monks Approved HTML tags (re markup) and On asking for help and How do I post a question effectively? (re the preferred approach to posting a question).

------

Now we could be of more help, I believe, were we provided samples (inside <code> tags, of course) of the keywords file and the logfile, where a few sample lines will suffice.

Update2: Added in light of the fine replies by AnomalousMonk and johngg

The analysis and suggestions by these stalwarts is right on the money. strict and warnings (esp in light of the bare "$found" and the improper use of split) meaningful variable names; and, in this case, use of a regex rather than index.

And given the disconnects between narrative and code, I've made some assumptions... reflected in these source files:

keywords.txt:

call,please,urgent

Skypelog.txt:

001 This is a convstn with "please" and "urgent" in it. SHOULD MATCH 002 This is another conversation without any of OP' selected keys 003 Call failed. Aborted. SHOULD MATCH. 004 Call includes "call." This is foolish but SHOULD MATCH 005 None of the keys appear here.

all of which is leadup to the code:

#!/usr/bin/perl # 862294 use strict; use warnings; my $file = "keywords.txt"; open(my $fh_keys, '<', $file) or die "Can't open $file for read: $!"; my $keys = <$fh_keys>; close ($fh_keys); chomp $keys; my @keys = split(',', $keys); my ($keyword1, $keyword2, $keyword3) = @keys; my $logfile = "Skypelogs.txt"; open (my $fh_log, '<', $logfile) or die $!; my @log = <$fh_log>; my $line; for $line(@log) { if ($line =~ /($keyword1|$keyword2|$keyword3)/i) { my $found .= $line . "\n"; print $found; } } =head OUTPUT: 001 This is a convstn with "please" and "urgent" in it. SHOULD MATCH 003 Call failed. Aborted. SHOULD MATCH. 004 Call includes "call." This is foolish but SHOULD MATCH =cut

Note, also, the manner in which this uses open() or die; the way we get the individual keywords out of $keys...@keys; and the reset of $found inside the loop (it would be very easy to write this in such a way as to print each line multiple times -- I know; I did it and then had to puzzle a bit to see why).

+ + votes to AnamalousMonk and johngg, please!


In reply to Re: How do I search for every occurrence of strings within an array and display them? by ww
in thread How do I search for every occurrence of strings within an array and display them? by rakesh01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.