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:
Expand|Select|Wrap|Line Numbers
- #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" ;
- }
Expand|Select|Wrap|Line Numbers
- 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