Where should i look for this answer:
Hi,
If you don't know the answer if you could please point me in the right direction
it would be appreciated. looking to search for more then 1 term at a time
with user interaction as shown in the script below. I have tried || but only
end up with everything being displayed. The current script displays the row
directly in front of the search term but I'm looking for it to display mutiple
search terms. Is this possible or does my entire script need to be re-written?
something along the lines of:
Search for: Name: || Number:
or
Search for: Name:, Number:
with the end results being something like:
John Wayne 225-3455-1234
Smith Berry 225-3143-2341
************************************************************
use Tie::File;
use Fcntl 'O_RDONLY';
$inputFile = 'source.txt';
$outputFile = 'output.txt';
print 'Enter 1 to print to screen, 2 to print to a file: ';
chomp($outputOpt = <STDIN>); #chomp removes newline from input string
print 'Search for: ';
chomp(my $searchPattern = <STDIN>);
tie @lines, 'Tie::File', $inputFile, mode => O_RDONLY or die "Can't open $inputFile for reading: $!\n";
if (@results = grep(/$searchPattern/,@lines)){
if ($outputOpt == 1){
print join("This box will close in 10 seconds\n",@results);
}
elsif ($outputOpt == 2){
open(OUTPUT, ">$outputFile") or die "Can't open $outputFile for writing: $!\n";
print OUTPUT join("\n",@results);
close(OUTPUT);
print "Everything is in a file called $outputFile\n";
}
}
else {
print "No match & this box will close in 10 seconds\n";
}
$num = 10;
while($num--){
sleep(1);
}