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

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.

Replies are listed 'Best First'.
Re^4: Help with Search String
by btobin0 (Acolyte) on Dec 07, 2007 at 11:59 UTC
    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

        Thanks you for your help. This works great. Overwrite would be better. To change that I would need to change the line:
        open my $outFH, q{>>}, $outFile
        Correct?
        i have a question I have put this together and it works great in Solaris, but in Linux the search results show everything. Got any ideas?
        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)/; 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}; # } close $dataFH or die qq{close: $dataFile: $!\n};