Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Help with Search String

by btobin0 (Acolyte)
on Dec 06, 2007 at 05:55 UTC ( [id://655285]=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 working on that opens a file searches for a word and then copies the information over to a secondary file. I am trying to make it to where the search looks towards a txt or dat file that has a list. What is the best approach to do this?
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;

Replies are listed 'Best First'.
Re: Help with Search String
by chrism01 (Friar) on Dec 06, 2007 at 06:45 UTC
    1. use 'or' instead of '||'
    2. use if ($_ =~ /Perl/ ) instead of if ($_ =~ "Perl")
    3. Check file close for errors (same as open)

    Cheers
    Chris
    PS: pls use code tags < code > < / code > (only without the spaces)

      I have done the following steps. Number 2 is where I need my help. I want to change it from
      ($_ =~ /Perl/ )
      to use a list of names in a .txt or .dat file. as the search string. Is this possible? say filename is names.txt
      Bob John Jenna Whoever
        Yes, very possible. Say that you have your names, one per line, in names.txt. You can read them into an array then use join to join the array members into a string separated by the pipe symbol ('|') which is the alternation metacharacter in a regex. Thus your pattern reads match Fred or Joe or ... or Pete. Like this.

        use strict; use warnings; my $namesFile = q{names.txt}; open my $namesFH, q{<}, $namesFile or die qq{open: $namesFile: $!\n}; my @names = <$namesFH>; close $namesFH or die qq{close: $namesFile: $!\n}; chomp @names; # Remove line terminators my $namesPatt = join q{|}, @names; my $dataFile = q{data.txt}; open my $dataFH, q{<}, $dataFile or die qq{open: $dataFile: $!\n}; while ( <$dataFH> ) { print qq{Found name $1\n} if /($namesPatt)/; } close $dataFH or die qq{close: $dataFile: $!\n};

        Note that I use parentheses in the regex to capture the name matched for later use in $1. I hope this is helpful.

        Cheers,

        JohnGG

        Update: Corrected poor punctuation/grammar.

Re: Search Param Help
by Gangabass (Vicar) on Dec 06, 2007 at 07:51 UTC

    If i understand you right than maybe this:

    my @search_list = qw( Perl PHP Ruby ); my $search_str = join( "|", @search_list ); if ( m/$search_str/ ) { $check = $_; }

    Note: that there is no need to write

    $_ =~ m/sometext/;

    $_ is used by default.

Re: Search Param Help
by sh1tn (Priest) on Dec 06, 2007 at 06:10 UTC
    Define hash with searched words and check for hash key existance:
    my %hash; @hash[qw(word1 word2)] = (1); if (exists($hash{$_})){ }
    perlre, perldsc


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found