Hi,
I am a beginner at perl. I am writing a program to open a file, take input from user for the words he wants to search within that file, print it then print the no of occurance of each word with respective line no.s in the file.
Below is the code written by me:
Upto this the program is doing fine but I am not able to count the no of occurance of each word given as input by user though I tried the following:
And I am not able to get the line no.'s for each word also.
Please help. Thanks in advance for any help.
Regards,
Dipak Kumar Singh
I am a beginner at perl. I am writing a program to open a file, take input from user for the words he wants to search within that file, print it then print the no of occurance of each word with respective line no.s in the file.
Below is the code written by me:
Code:
#printing the file
open (MYFILE, 'C:\Documents and Settings\dsingh20\My Documents\Perl_Task.txt');
while (<MYFILE>)
{
chomp;
print "$_\n";
}
#taking input from user and printing the same
print "Enter The Words You want to Search - " ; # printing on the STDOUT
$line = <STDIN>;#read line
$line =~ s/(^\s+)|(\s+$)//g;#remove lead and trail white space
@data = split(/\s+/,$line);#split on white space into array
print " Words entered by the user are: \n\n";
foreach $data (@data)
{
print" $data \n" ;
}
Code:
foreach $data (@data)
{
my $count = 0;
while (/$data/ig, <MYFILE>)
{
$count++;
}
print "$count occurance found:;
}
Please help. Thanks in advance for any help.
Regards,
Dipak Kumar Singh
Comment