in reply to Searching through a word file using an array element

your code is a little hard to understand!!!
please let me know if this solves your problem
#! /usr/bin/perl -w use strict; my @arr=('perl','monks','website'); our @files=('file1.txt','file2.txt','file3.txt'); open(LOG,">","output.txt"); my $file; foreach $file(@files) { open(FILE,"<",$file); my @lines=<FILE>; close(FILE); my $count=0; my $line; foreach $line(@lines) { $count++; if($line =~ /.*$arr[0]/) { print LOG "$arr[0] found in $file at line number $count\n"; } if($line =~ /.*$arr[1]/) { print LOG "$arr[1] found in $file at line number $count\n"; } if($line =~ /.*$arr[2]/) { print LOG "$arr[2] found in $file at line number $count\n"; } } } close(LOG);
file1.txt
ansh batra perl postgres apache monks
file2.txt
ansh batra is in perl monks apache postgres
file3.txt
website monks perl website ansh ansh website
output.txt
perl found in file1.txt at line number 3 monks found in file1.txt at line number 6 perl found in file2.txt at line number 1 monks found in file2.txt at line number 1 website found in file3.txt at line number 1 perl found in file3.txt at line number 2 monks found in file3.txt at line number 2 website found in file3.txt at line number 2 website found in file3.txt at line number 5
thanks!!!
ansh batra

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.