Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How do I search for every occurrence of strings within an array and display them?

by ww (Archbishop)
on Sep 27, 2010 at 21:27 UTC ( [id://862305]=note: print w/replies, xml ) Need Help??


in reply to How do I search for every occurrence of strings within an array and display them?

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://862305]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found