in reply to Re: Help with Search String
in thread Help with Search String

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

Replies are listed 'Best First'.
Re^3: Help with Search String
by johngg (Canon) on Dec 06, 2007 at 10:00 UTC
    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.

      Now I have edited a few things and it now prints everything in the list. It print the output with the right response. but when saved it prints all 4 sentences. What am I doing wrong?
      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{data2.txt}; open my $dataFH, q{<}, $dataFile or die qq{open: $dataFile: $!\n}; while ( <$dataFH> ) { print qq{Found name $_\n} if /($namesPatt)/; open FILE, ">>/home/btobin/data3.txt" or die "Unable to Open: $!"; print FILE "$_\n"; <--- somehow this is wrong close FILE; } close $dataFH or die qq{close: $dataFile: $!\n};
      Sentences used:
      This is a data file created to learn how to use Perl . This is how Bob found his way. I wonder what Jonny is going to do. This is a test.
        Your error is that you do print FILE "$_\n"; unconditionally so that every line of your input file is printed to your output file. There are other problems with your code.

        • Do you really want to append your results to data3.txt rather than overwriting the file each run?

        • Whether you append or overwrite, open and close the file outside of the while loop.

        • Test for the success of your close operation.

        • It is recommended to use lexical rather than bare word filehandles and the three-argument form of open.

        The code becomes

        my $outFile = q{/home/btobin/data3.txt}; open my $outFH, q{>>}, $outFile or die qq{open: $outFile: $!\n}; while ( <$dataFH> ) { print $outFH qq{Found name $_\n} if /($namesPatt)/; } close $outFH or die qq{close: $outFile: $!\n};

        I hope this is helpful.

        Cheers,

        JohnGG