Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by btobin0 (Acolyte)
on Dec 05, 2007 at 07:23 UTC ( [id://655044]=perlquestion: print w/replies, xml ) Need Help??

btobin0 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that I am using on name to search with. I want to use a .txt file or something so I can have a list of names. How do I do this?
  • Comment on How can I use a text file as a search string?

Replies are listed 'Best First'.
Re: How can I use a text file as a search string?
by GrandFather (Saint) on Dec 05, 2007 at 09:10 UTC

    Maybe you could show us the pertinent part of your "script that I am using on name to search with" and describe how you need to alter it to use the list of names.


    Perl is environmentally friendly - it saves trees
Re: How can I use a text file as a search string?
by Gangabass (Vicar) on Dec 05, 2007 at 08:04 UTC

    Open your file for reading (open), read each line from your file (while and <>), remove trailing new line character (chomp) and search by result string.

      Would I need an open statement for each file or would chomp have a statement to the other file?

        You need open statement for each file.

Re: How can I use a text file as a search string?
by CountZero (Bishop) on Dec 05, 2007 at 10:25 UTC
    An example of the text file you will be using would be handy to show to us as well.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How can I use a text file as a search string?
by perlfan (Vicar) on Dec 05, 2007 at 15:11 UTC
    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.
      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;
Re: How can I use a text file as a search string?
by dwu (Monk) on Dec 06, 2007 at 06:41 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found