Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How can I use a text file as a search string?

by perlfan (Curate)
on Dec 05, 2007 at 15:11 UTC ( #655104=note: print w/ replies, xml ) Need Help??


in reply to How can I use a text file as a search string?

If you are searching for literal strings, and don't *really* need regexs, then you could create a hash, where each name in the .txt file is one you're expecting. To search, you'd just use exists($hash{$name_to_check}) to see it was in your text file.

my @names_to_check = qw/ bob sue joe /; my %validnames = (); # build lookup while(<>) { chomp; $validnames{$_}++; # also filters dups in *.txt } # check names for (@names_to_check) { print "$_ is okay\n" if (exists($validnames{$_})); }
The <> of course implies STDIN, so you'd have to redirect or pipe the contents of *.txt into the script at the commandline.


Comment on Re: How can I use a text file as a search string?
Select or Download Code
Re^2: How can I use a text file as a search string?
by btobin0 (Novice) on Dec 06, 2007 at 06:30 UTC
    This is what ihave so far. the part that is asking to look for perl. I want to change it to where it looks in a list like a txt or dat file.
    open FILE, "/home/btobin/data.txt" || die "Unable to Open: $!"; while (<FILE>) { if ($_ =~ "Perl") { $check = "$_"; } } close FILE; open FILE2, ">>/home/btobin/data2.txt" || die "Unable to Open: $!"; print FILE2 "$check\n"; close FILE2;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-07-02 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls